Initialization of Structure

In article What is Structure , you learned that, why structures are important, how to define the structures and how member variables can be accessed. In this article you will learn the different ways to initialize the instance of the structure and how to assign the values to the member variables. Let’s start with structure student.

struct Student
{
char name[20];
int ID;
int age;
long phone;
}

There are different ways to initialize the instance of the structure.

You can initialize the instance of this structure in normal way as you initialize other variables for example int i; float j; Student std1; Student std2;

You can Initialize the variable on the same place where you have defined your structure body. Write the name of variable just after ending bracket.

struct Student { char name[20]; int ID; int age; float phone; }std3;

std3 is declared at the same place where the structure Student is defined. Now whenever you will use this variable, it will contain all members of the structure: name, ID, age , phone.

You have different ways to initialize the structure , in the same way you have also different ways to assign values to the member variables of structure. Suppose you have one instance of Student std1. You know that you can access the member variable by using “.” dot operator so you can initialize variables as follow;

Std1.Id = 123; Std.age=32;

You can assign values to member variables on the same place where you write the structure body. After closing bracket, you can initialize your structure and you can assign the values in coma separated array {“Abid”, 123 , 33, 123123}.

You must be careful while initializing the member variables using array. You must pass the values in the same order as it is declared in structure. If you will assign integer value in character or other way around, you will get compile time error.

{123 , “Abid”, 3, 123123}; // wrong order

One more thing which may not cause the error but can come up with unexpected results.

Example: In struct student, there are two integer variables ID and age std2 = {“Abid”, 123 , 33, 123123};

now student name is abid, ID is 123, age is 33 and phone number is 123123. every thing looks fine but what if you assign values in incorrect order; std2 = {“Abid”, 33 , 123, 123123};

It will not cause any error but student ID will be 33 and student age will be 123 years (student abid looks very old, 🙁 please don’t do that). There are some benefits by assigning values in this way but it is not a good practice. If your structure has 10 or more variables in it then there will be much more trouble to handle all variables.

You can initialize as many variables of your structure as you want and you can assign the values to your members on the same time. Copy the following example and run it your editor.

struct Student // name of the structure Student
{
char name[20]; // member variable char array for name
int ID; // member variable int for ID
int age; // member variable int for age
long phone; // member variable long for phone
}std3 = {"Khan", 110 , 23,112322}, std1 = {"Abid",100,33,123111};
int main ()
{
printf("\nStudent name is %s",std1.name);
printf("\nStudent ID is %d",std1.ID);
printf("\nStudent age is %d",std1.age);
printf("\nStudent phone is %d\n",std1.phone);
printf("\nStudent name is %s",std3.name);
printf("\nStudent ID is %d",std3.ID);
printf("\nStudent age is %d",std3.age);
printf("\nStudent phone is %d\n",std3.phone);
return 0;
}