Jan 4, 2016

C Programming #57: Structure - partial initialization

This article discusses about partial initialization of structure. Sometimes requirement would be such that only few member variable initial values are known during time of definition. In such cases partial initialization will comes in very handy.


Lets take following structure declaration

typedef struct student{
   char name[20];
   int age;
   int class;
}student_t;

Say we want to initialize only name of the student (we are not sure of age and class). Then initialization of the variable can be done as follows.

student_t a = {"Ram"};
student_t b = {"Mason"};

Hence a.name will be initialized to "Ram" and b.name will be initialized to "Mason". Even though age and class are not initialized; compiler will implicitly put a initial value of 0. (Note this implicit rule applies to only partially initialized structure, for uninitialized structure it wont come into picture at all). Hence in case of partially initialized structure, uninitialized member variable of numerical type (char, int, long, float, double) will be initialized to 0, and pointers will be initialized to NULL.

Lets take one more case where in class is to be initialized with 8 and name and age is unknown. In such case we could do it as follows.

student_t a = {"", 0, 8};

In above usage even though only initial value of class is know still we need to supply initial value of ""(empty string) to name and 0 as age as initial value. Initial values are always taken in order in which the declaration of the variable is done in the structure declaration.

Newer standard of C which got formulated in 1999 commonly referred to as C99 gives us one more interesting syntax for initialization of above case.

student_t a = { .class = 8 };

In above case we are telling C compiler to only initialize class member variable to 8. Even the full initialization could be done as follows.

student_t a = {.name = "Ram", .age = 7, .class = 2};

Advantages are
  1. It gives very clear idea to programmer what member variable is initialized with what.
  2. If someone plans to change the order of member in structure declaration; same intilization would still hold good.
  3. It always any member to have initial value.


Disadvantages are
  1. If you compile with older compiler which has not implemented the C99 standard it will not work.

C99 is supported by most of C compiler, hence selective initialization should be preferred.

Following program uses partial initialization.


#include <stdio.h>

typedef struct student {
   char name[20];
   int age;
   int class;
}student_t;

void print_student(student_t* ps);

int main()
{
   student_t a = {.name = "Ram", .class = 2, .age = 7};
   student_t b = {"Mason", 9, 4};

   print_student(&a);
   print_student(&b);

   return 0;
}

void print_student(student_t* ps)
{
  printf("Student %s age is %d and studies in %d\n",
         ps->name, ps->age, ps->class);
}

Output of the above program is

Student Ram age is 7 and studies in 2
Student Mason age is 9 and studies in 4

Links

Next Article - C Programming #58: Structure - bit-fields
Previous Article - C Programming #56: Structure - forward declaration 
All Article - C Programming

No comments :

Post a Comment