16.3 – Initializing a struct variable

by subbu on September 15, 2014

As we know, assigning a value while declaration of a variable is called initializing. In this session we will discourse different ways of initializing a struct variable and issues involved.

Initializing a struct variable

A struct variable can be initialized with a set of values as initialization of an array.

struct product
{
 int pcode;
 char pname[20];
 float price;
 int qty;
 float bill;
};

Here any variable belongs to “struct product” will have the members pcode of int type, pname of string type, price of float type, qty of int type and bill of float type.

While initializing a set of values to the struct variable make sure that, the order of values must match with the order of members in the struct definition.

struct product a={1000,"cock",25.50,2,51.00};

Here the values would be properly assigned to the struct variable “a”

initializing a struct variable

#include<stdio.h>
struct product
{
 int pcode;
 char pname[20];
 float price;
 int qty;
 float bill;
};
int main()
{
 struct product a={1000,"cock",25.50,2,51.00};
 printf("Product Code: %d",a.pcode);
 printf("\nProduct Name: %s",a.pname);
 printf("\nPrice: %f",a.price);
 printf("\nQuantity: %d",a.qty);
 printf("\nBill: %f",a.bill);
 return 0;
}

Output:
Product Code: 1000
Product Name: cock
Price: 25.500000
Quantity: 2
Bill: 51.000000

It will leads to error if the order of values mismatch with the order of members in the struct definition. We will see the fact with an example

#include<stdio.h>
struct product
{
 int pcode;
 char pname[20];
 float price;
 int qty;
 float bill;
};
int main()
{
 struct product a={1000,25.50,"cock",2,51.00};
 printf("Product Code: %d",a.pcode);
 printf("\nProduct Name: %s",a.pname);
 printf("\nPrice: %f",a.price);
 printf("\nQuantity: %d",a.qty);
 printf("\nBill: %f",a.bill);
 return 0;
}

Output:
//error

In the above example, price and pname are assigned in the order instead of pname and price, results error

NULL struct variable:

If all the members of a struct variable are assigned with zeros then the struct variable is called NULL variable. We can assign every member of a struct variable with zero by initializing the struct variable with zero set.

struct box
{
 int b;
 int w;
 int h;
};
struct box x={0};

Here all the members of struct variable “x” are initialized with 0s

struct box y={1};

Here only the first member y. b is initialized with 1 and other members are initialized with 0s

#include<stdio.h>
struct box
{
 int b;
 int w;
 int h;
};
int main()
{
 struct box x={0};
 struct box y={1};
 printf("Sides of first box %d\t%d\t%d",x.b,x.w,x.h);
 printf("\nSides of second box %d\t%d\t%d",y.b,y.w,y.h);
 return 0;
}

Output
Sides of first box  0    0      0
Sides of second box 1   0       0

Initialization in C99:

From C99 onwards the initialization sequence can be changed by specifying the name of member in initialization statement

struct box x={.w=20,.b=10,.h=45};

Here the first member “b” is initialized with 10, the second member “w” is initialized with 20 and the third member “h” is initialized with 45

struct box x={.w=20};

Here only the second member “w” is initialized with 20, first and third members “b”, “h” are initialized with 0s

#include<stdio.h>
struct box
{
 int b;
 int w;
 int h;
};
int main()
{
 struct box x={.w=20,.b=10,.h=45};
 struct box y={.w=20};
 printf("Sides of first box %d\t%d\t%d",x.b,x.w,x.h);
 printf("\nSides of second box %d\t%d\t%d",y.b,y.w,y.h);
 return 0;
}

Execution in ubuntu gcc:

$gcc –o demo demo.c
$./demo
Sides of first box    10   20   45
Sides of second box   0    20    0
$

Note: If members of a struct variable are not assigned with any values then they are stored with garbage values.

Previous post:

Next post: