Structure Initialization


The structure variables can also be initialized like variable of any other data types. A structure variable is initialized with values that are to be assigned to its members within the braces during its definition. The values should appear in the same order as the order of the member of the structure.

Let us consider an example of employee structure which is declared as follows,
struct employee
{
            int temp_no;
            char emp_name [20];
            int dept_code;
            double salary;
}
We can define and initialize structure variable emp1 of type employee as follows,
employee emp1 = { 101, “ABC”, 05, 27152.50 };

The value to be assigned to structure variable emp1 is enclosed in curly braces separated by commas. The member emp_no, emp_name, dept_code, salary of variable emp1 are initialized with 101,ABC,05,27152.50 respectively. Initialization should be performed at the time of defining structure variable. 

Previous                                                                                    Next


Powered by Blogger.