Structure in C programming

Structure is a group of variables of different data types represented by a single name.

Lets say we need to store the data of students like student name, age, address, id etc. One way of doing this would be creating a different variable for each attribute, however when you need to store the data of multiple students then in that case, you would need to create these several variables again for each student. This is such a big headache to store data in this way.

We can solve this problem easily by using structure. We can create a structure that has members for name, id, address and age and then we can create the variables of this structure for each student. This may sound confusing, do not worry we will understand this.

How to create a structure in C Programming

We use struct keyword to create a structure in C. The struct keyword is a short form of structured data type.

struct struct_name {
   DataType member1_name;
   DataType member2_name;
   DataType member3_name;
   
};

Here struct_name can be anything of your choice. Members data type can be same or different. Once we have declared the structure we can use the struct name as a data type like int, float etc.

First we will see the syntax of creating struct variable, accessing struct members etc.

How to declare variable of a structure?

struct  struct_name  var_name;

or

struct struct_name {
   DataType member1_name;
   DataType member2_name;
   DataType member3_name;
   
} var_name;

How to access data members of a structure using a struct variable?

var_name.member1_name;
var_name.member2_name;

How to assign values to structure members?

There are three ways to do this.
1) Using Dot(.) operator

var_name.memeber_name = value;

2) All members assigned in one statement

struct struct_name var_name = 
{value for memeber1, value for memeber2 so on for all the members}

Nested Structure in C: Struct inside another struct

You can use a structure inside another structure, which is fairly possible. As I explained above that once you declared a structure, the struct struct_name acts as a new data type so you can include it in another struct just like the data type of other data members. Sounds confusing? Don’t worry. The following example will clear your doubt.

Example of Nested Structure in C Programming

Lets say we have two structure like this:
Structure 1: stu_address

struct stu_address
{
     int street;
     char *state;
     char *city;
     char *country;
}

Structure 2: stu_data

struct stu_data
{
    int stu_id;
    int stu_age;
    char *stu_name;
    struct stu_address stuAddress;
}

As you can see here that I have nested a structure inside another structure.

Assignment for struct inside struct (Nested struct)

Lets take the example of the two structure that we seen above to understand the logic

struct  stu_data  mydata;
mydata.stu_id = 1001;
mydata.stu_age = 30;
mydata.stuAddress.state = "UP"; //Nested struct assignment
..

How to access nested structure members?

Using chain of “.” operator.
Suppose you want to display the city alone from nested struct –

printf("%s",  mydata.stuAddress.city);

Use of typedef in Structure

typedef makes the code short and improves readability. In the above discussion we have seen that while using structs every time we have to use the lengthy syntax, which makes the code confusing, lengthy, complex and less readable. The simple solution to this issue is use of typedef. It is like an alias of struct.

Code without typedef

struct home_address {
  int local_street;
  char *town;
  char *my_city;
  char *my_country;
};
...
struct home_address var; 
var.town = "Agra";

Code using tyepdef

typedef struct home_address{
  int local_street;
  char *town;
  char *my_city;
  char *my_country;
}addr;
..
..
addr var1;
var.town = "Agra";

Instead of using the struct home_address every time you need to declare struct variable, you can simply use addr, the typedef that we have defined.

Designated initializers to set values of Structure members

We have already learned two ways to set the values of a struct member, there is another way to do the same using designated initializers. This is useful when we are doing assignment of only few members of the structure.


 

For any query or issue, feel free to discuss on http://discuss.eduguru.in