Type Here to Get Search Results !

Structure Initialization

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.
Note that the compile time initialization of a structure variable must have the following elements:
  1. The keyword struct 
  2. The struct tag name. 
  3. The name of the variable to be declared. 
  4. The assignment operator (=) 
  5. A set of values for the members of the structure variable, separated by commas and enclosed in braces. 
  6. A terminating semicolon.

Top Post Ad

Below Post Ad