Structure is a group of variables of different data types represented by a single name. Let’s take an example to understand the need of a structure in C programming.
Let’s 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 with the help of example.
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 is the name of the structure, this is chosen by the programmer and can be anything. However, always choose meaningful, short and easy to read names and avoid using keywords and reserved words as structure names as those are not allowed. 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 and then we will see a complete example.
struct struct_name var_name;
struct struct_name < DataType member1_name; DataType member2_name; DataType member3_name; … >var_name;
var_name.member1_name; var_name.member2_name; …
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 =3) Designated initializers – We will discuss this later at the end of this post.
In this example, we have created a structure StudentData with three data members stu_name , stu_id and stu_age . In this program, we are storing the student name, id and age into the structure and accessing structure data members to display these values as an output.
#include /* Created a structure here. The name of the structure is * StudentData. */ struct StudentData< char *stu_name; int stu_id; int stu_age; >; int main() < /* student is the variable of structure StudentData*/ struct StudentData student; /*Assigning the values of each struct member here*/ student.stu_name = "Steve"; student.stu_id = 1234; student.stu_age = 30; /* Displaying the values of struct members */ printf("Student Name is: %s", student.stu_name); printf("\nStudent Id is: %d", student.stu_id); printf("\nStudent Age is: %d", student.stu_age); return 0; >
Student Name is: Steve Student Id is: 1234 Student Age is: 30
You can use a structure inside another structure, this is called nesting of structures. As I explained above, once a structure is declared, 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.
Let’s say we have two structure like this: The second structure stu_data has stu_address as a data member. Here, stu_data is called outer structure or parent structure and stu_address is called inner structure or child structure.
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.
Let’s take the example of the two structure that we seen above to understand the logic. We are using the . (dot) operator twice to access the data member of inner structure.
struct stu_data mydata; mydata.stu_id = 1001; mydata.stu_age = 30; mydata.stuAddress.state = "UP"; //Nested struct assignment ..
Similarly other data members of inner structure can be accessed like this:
mydata.stuAddress.city = "Delhi"; mydata.stuAddress.country = "India";
Using chain of “.” operator. Let’s say you want to display the city alone from nested struct:
printf("%s", mydata.stuAddress.city);
Let’s see the complete program:
#include struct stu_address < int street; char *state; char *city; char *country; >; struct stu_data < int stu_id; int stu_age; char *stu_name; struct stu_address stuAddress; >; int main()
Output:
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.
You can read the typedef in detail here.
An array of structures is an array with structure as elements.
For example:
Here, stu[5] is an array of structures. This array has 5 elements and these elements are structures of the same type “student”. The element s[0] will store the values such as name, rollNum, address & marks of a student, similarly element s[1] will store these details for another student and so on.
struct student < char name[60]; int rollNum; char address[60]; float marks; >stu[5];
To learn more about structures in C with complete example: Refer this guide.
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. In the following example the structure variable s2 has only one member assignment.
#include struct numbers < int num1, num2; >; int main() < // Assignment using using designated initialization struct numbers s1 = ; struct numbers s2 = ; printf ("num1: %d, num2: %d\n", s1.num1, s1.num2); printf ("num1: %d", s2.num2); return 0; >
num1: 11, num2: 22 num1: 30