Declaring a Structure Variable:
--> By defining a structure we have only created a format, the actual use of structures will be when we declare variables based on this format.
--> We declare structure variables in 2-ways:
1)With Structure definition
2)Using the Structure tag
1.With Structure definition:-
struct student
{
char name[20];
int rollno;
float marks;
}stu1, stu2, stu3;
--> Here stu1, stu2, stu3 are variables of type struct "student". when we declare a variable while defining the structure template, the tagname is optional.
--> So, we can also declare them as:
struct
{
char name[20];
int rollno;
float marks;
}stu1, stu2, stu3;
--> If we declare variables in this way, then we will not be able to declare other variables of this structure type anywhere else in the program
--> So, although the tagname is optional it is always better to specify a "tagname" for the structure.
2. Using Structure Tag:
We can also declare structure variables using structure tag. This can be written as:
struct student
{
char name[20];
int rollno;
float marks;
};
struct student stu1, stu2;
struct student stu3;
--> Here stu1, stu2 and stu3 are structure variables that are declared using the structure tag "student".
--> Declaring a structure variable reserves "space in memory". Each structure variable declared to be of type struct student has 3 members. name, rollno, marks.
--> The "compiler" will "reserve space" for each variable sufficient to hold all the members.
Example:
each variable of type struct student will occupy 26(20+2+4) bytes.
--> By defining a structure we have only created a format, the actual use of structures will be when we declare variables based on this format.
--> We declare structure variables in 2-ways:
1)With Structure definition
2)Using the Structure tag
1.With Structure definition:-
struct student
{
char name[20];
int rollno;
float marks;
}stu1, stu2, stu3;
--> Here stu1, stu2, stu3 are variables of type struct "student". when we declare a variable while defining the structure template, the tagname is optional.
--> So, we can also declare them as:
struct
{
char name[20];
int rollno;
float marks;
}stu1, stu2, stu3;
--> If we declare variables in this way, then we will not be able to declare other variables of this structure type anywhere else in the program
--> So, although the tagname is optional it is always better to specify a "tagname" for the structure.
2. Using Structure Tag:
We can also declare structure variables using structure tag. This can be written as:
struct student
{
char name[20];
int rollno;
float marks;
};
struct student stu1, stu2;
struct student stu3;
--> Here stu1, stu2 and stu3 are structure variables that are declared using the structure tag "student".
--> Declaring a structure variable reserves "space in memory". Each structure variable declared to be of type struct student has 3 members. name, rollno, marks.
--> The "compiler" will "reserve space" for each variable sufficient to hold all the members.
Example:
each variable of type struct student will occupy 26(20+2+4) bytes.
Next
0 comments:
Post a Comment