Structure Initialization
Like any other data type, a structure variable can be initialized at
declaration time.
Example:
main()
{
struct student
{
int
height;
float weight;
} s1={180,60.25};
}
This assigns the value 180 to height of sl and 60.25 to weight of s1.
There is one to one connection between the members and their
initializing values. Another method for initializing a structure
main()
{
struct employee
{
char ename[20];
int eno;
float esalary;
};
struct employee e1= { "RAMU", 10,
10000 };
struct employee e2= { "SOMU", 20,
20000 };
}
Another method to initialize a structure variable outside the
function is as shown.
struct employee
{
char ename[20];
int eno;
float esalary;
} e1 = { "RAMU", 10, 10000 }; //Global declaration of
structure main()
{
struct emproyee e2 = { "SOMU", 20, 20000 }; // declaration
and initialization of structure variables
}
- C language does not permit the initialization of individual structure members within the template
- The initialization must be done only in the declaration of the structure variables.
- The keyword struct
- The struct tag name.
- The name of the variable to be declared.
- The assignment operator (=)
- A set of values for the members of the structure variable, separated by commas and enclosed in braces.
- A terminating semicolon.