The syntax of initializing structure variables is similar to that of arrays.The initializing values can only be constant expressions.
struct student {
char name[20];
int rollno;
float marks;
}stu1={"Perry",205,17};
struct student stu2 = {"John","24,67.5};
Here value of members of stu1 will be "Perry" for name,205 for rollno ,17 for marks. The values of members of stu2 will be "John" for name ,24 for rollno ,67.5 for marks.
NOTE:: We cannot initialize members while defining the structure.
struct student {
char name[20];
int rollno=20 ; //invalid
float marks;
}stu1;
This is invalid because there is no variable called rollno, and no memory is allocated for structure definition.
If the number of initializers is less than the number of members then the remaining members are initialized with zero. For example if we have this initialization
struct student stu1 = {"Perry"};
Here the members rollno and marks of stu1 will be initialized to zero. This is equivalent to the initialization
struct student stu1 = {"Perry",0.0};
No comments:
Post a Comment