in , ,

C struct

C struct
C struct

C struct: In this tutorial, you’ll find out about struct types in C Programming. You will learn to characterize and use structures with the help of examples.

In C programming, a struct (or structure) is an assortment of factors (can be of various sorts) under a single name.


In this article, you will learn-

How to define structures?

Before you can create structure variables, you need to define its data type. To define a struct, the struct keyword is used.

Syntax of struct

struct structureName 
{
    dataType member1;
    dataType member2;
    ...
};

Here is an example:

struct Person
{
    char name[50];
    int citNo;
    float salary;
};

Here, a derived type struct Person is defined. Now, you can create variables of this type.


Create struct factors

At the point when a struct type is proclaimed, no capacity or memory is dispensed. To apportion memory of a given structure type and work with it, we need to create variables.

Here’s how we create structure variables:

struct Person
{
    char name[50];
    int citNo;
    float salary;
};

int main()
{
    struct Person person1, person2, p[20];
    return 0;
}

Another way of creating a struct variable is:

struct Person
{
    char name[50];
    int citNo;
    float salary;
} person1, person2, p[20];

In both cases, two variables person1, person2, and an array variable p having 20 elements of type struct Person are created.


Access members of a structure

There are two types of operators used for accessing members of a structure.

. – Member operator
-> – Structure pointer operator (will be discussed in the next tutorial)
Suppose, you want to access the salary of person2. Here’s how you can do it.

person2.salary

Example: Add two distances

// Program to add two distances (feet-inch)
#include <stdio.h>
struct Distance
{
    int feet;
    float inch;
} dist1, dist2, sum;

int main()
{
    printf("1st distance\n");
    printf("Enter feet: ");
    scanf("%d", &dist1.feet);

    printf("Enter inch: ");
    scanf("%f", &dist1.inch);
    printf("2nd distance\n");

    printf("Enter feet: ");
    scanf("%d", &dist2.feet);

    printf("Enter inch: ");
    scanf("%f", &dist2.inch);

    // adding feet
    sum.feet = dist1.feet + dist2.feet;
    // adding inches
    sum.inch = dist1.inch + dist2.inch;

Output

1st distance
Enter feet: 12
Enter inch: 7.9
2nd distance
Enter feet: 2
Enter inch: 9.8
Sum of distances = 15'-5.7"

Keyword typedef

We use the typedef keyword to create an alias name for data types. It is commonly used with structures to simplify the syntax of declaring variables.

This code

struct Distance{
    int feet;
    float inch;
};

int main() {
    struct Distance d1, d2;
}

is equivalent to

typedef struct Distance{
    int feet;
    float inch;
} distances;

int main() {
    distances d1, d2;
}

Nested Structures

You can create structures within a structure in C programming. For example,

struct complex
{
 int imag;
 float real;
};

struct number
{
   struct complex comp;
   int integers;
} num1, num2;

Suppose, you want to set an imag of the num2 variable to 11. Here’s how you can do it:

num2.comp.imag = 11;

Why structs in C?

Assume, you need to store data about an individual: his/her name, citizenship number, and compensation. You can make various variables name, citNo, and pay to store this information.

What if you need to store information of more than one individual. Presently, you have to make various factors for every data per individual: name1, citNo1, salary1, name2, citNo2, salary2, etc.

A better approach is to have an assortment of all related data under a solitary name Person structure and use it for each individual.


Please feel free to give your comment if you face any difficulty here.

salman khan

Written by worldofitech

Leave a Reply

String Examples in C Programming

String Examples in C Programming

C structs and Pointers

C structs and Pointers