C: Structure Initialization

There are several ways by which a structure can be initialized. In the first way, we first declare the structure variable and initialize the fields of the structure with its name along with the variable name.
i.e., Suppose consider a structure ‘student

structure student {

   int roll;

   int class;

   char name[25];

};

Now we declare variable for the structure student st1
Now to initialize this variable, we access the elements of the structure using the variable
i.e.,

      st1.roll  = 21;

      strncpy (st1.name, "Albert", 10);

There can be case in which we do not initialize some elements of the structure, like in the above case, we did not initialize the element class for the variable st1. This can be considered as a big disadvantage. Because we may forget initializing some fields.

In the second way, we initialize the structure elements using the set notation.
like the variable st2 can be initialized as

     struct student st2 = {22, 10, "Alan"};

Unlike the first way, where we can initialize the elements of the structure in any order, here we must remember the order of the elements in the structure. But chances of not initializing a particular element is very less.

The following program demonstrates these two ways of structure initialization. Also check the way an array of structures is initialized.

/*Different Ways of structure initialization*/#include <stdio.h>

#include <string.h>

#define NAME_LEN 25

typedef unsigned short age_t;

typedef unsigned int roll_t;

typedef struct student{

char name[NAME_LEN];

roll_t rno;

age_t age;

}student;

int main()

{

/*

* Method 1a:Commonly seen initialization of a structure
    */student st;

strcpy

    (st.name,”Albert”); st.age=23; st.

rno

    =1034;
printf("%s %hi %u\n\n",st.name,st.age,st.rno);

/*

* Method 1b: For initialing an array of structures

*Commonly seen initialization of a structure

*/
    student st1 [2];

strcpy

    (st1[0].name,”Albert”);st1[0].age=23; st1[0].

rno

    =1034;
strcpy(st1[1].name,"Alvin");

st1[1].age=24;

st1[1].rno=1035;

printf("%s %hi %u\n%s %hi %u\n\n",st1[0].name,st1[0].age,

st1[0].rno,st1[1].name,st1[1].age,st1[1].rno);

/*

* Method 2a:Initializing like a set, the only requirement is

* that the order by which these elements are entered

* should be as in the declaration

*/
    student st2={“Alvin”,1035,22};

printf

    (“%s %hi %u\n\n”,st2.name,st2.age,st2.rno);
/*

* Method 2b: For Initializing an array of structures

*/
    student st2b[2]={ {“Alan”,1036,23}, {“Eric”,1037,22}};
printf("%s %hi %u\n%s %hi %u\n\n",st2b[0].name,st2b[0].age,

st2b[0].rno, st2b[1].name,st2b[1].age,st2b[1].rno);

}

 

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s