Size of structure is the addition of individual sizes of each data member of that structure.
We can calculate size of structure by using sizeof operator.
sizeof is an operator which is used to calculate size of any data type or variable.
So here size of structure variable s1 can be calculated as:
sizeof(s1)=sizeof(Rno)+sizeof(Sname)+sizeof(Percentage) sizeof(s1)= 2 + 20 + 4
sizeof(s1)=26 bytes
( if int occupies 2 bytes)
Initialization of a structure variable
Assigning default values to a structure variable at the time of its declaration is called as initialization of structure variable. Values get assigned to the structure data members from first to last so it should match the data type of the data members and it should follow order. Structure data members get initialized using curly brackets ‘{ }’.
If we initialize less number of members remaining members will get initialized to 0 and if we initialize more number of members then compiler will give an error.
There are two different ways to initialize structure variable
1) struct StudentInfo
{ int Rno;
char Sname[20];
float Percentage;
}s1={1,‟xyz”,80.25};
struct StudentInfo s1={1,‟xyz”,80.25};
Comments
Post a Comment
Please give us feedback through comments