Pages

Friday, December 28, 2012

Defining a Structure


Structure definition is
struct tagname
{
datatype member 1;
datatype member 2;
................................
datatype member N;
};
Here struct is a keyword. which tells the compiler that a structure is being defined member 1 member 2,.......... member N are known as members of the structure and declared inside curly braces.There should be semicolon at the end of the curly braces.These members can be any data type like int,float,char ,array ,pointer or another structure type. tagname is the name of the structure.and it is used further in program to declare variables of this structure type.
Note::
 It is important note that definition of structure template does not reserve any space in memory for the members,Space reserved only when declare variable of structure type.
The member name inside the structure should be different from one another but these name can be similar to any other variable name declared outside of the structure.The member of two different structure may also be same.
Lets take example of defining a structure template.
struct student {
char name[20];
int rollno;
float marks;
};
Here student is structure tag and these three are member of structure.

No comments:

Post a Comment