Pages

Friday, December 28, 2012

Size of Structure - C Programming

To find out the size of structure by sizeof operator,we can either use the structure variable name or the tagname with the  struct keyword. For Example -::


sizeof(struct student)
sizeof(stu1) // Here stu1 is variable structure type



struct student {
   char name[20]; //Occupy 1 * 20 = 20 bytes
   int rollno;       //4 bytes
};
struct student stu1;

printf("size of structure  is %d\n",sizeof(struct student));
printf("size of structure is %d\n",sizeof(stu1));


Output ::
 24
 24

Size of datatype depend on compiler


Storage of structures in memory,C Programming


The main point is that structure members are stored in consecutive memory locations.

main()
{
struct student {
char name[20];
int rollno;
float marks;
}stu;
printf("Address of name = %u\n",stu.name);
printf("Address of rollno = %u\n",stu.rollno);
printf("Address of marks= %u\n",stu.marks);
}
OutPut::
Address of name = 2000
Address of rollno = 2005
Address of marks = 2007;
See Below figure more understanding ..!!(Output may be different on different machines)


g2991

Assignment of structure variables - C Programming


We can assign of a structure variable to another structure variable , if both variable are defined of the same structure type.

Example::
struct student {
char name[20];
int rollno;
float marks;
};

main()
{
struct student stu1 = {"Perry",25,20};
struct student stu2;
stu2 = stu1;
printf("Stu1 :: %s %d %f\n",stu1.name,stu1.rollno,stu1.marks};
printf("Stu2:: %s %d %f\n",stu2.name,stu2.rollno,stu2.marks};
}

OutPut::
        Stu1:: Perry 25 20
        Stu2:: Perry 25 20

Accessing Members of a Structure - C Programming


For accessing any member of a structure variable,we use the dot (.) operator which is also known as the period or membership operator.The format for accessing a structure member is -

  structvariable.member

Here on the left side of the dot there should be a variable of structure variable of structure type and right side there should be the name of a member of that structure.

For example ::
struct student {
char name[20];
int rollno;
float marks;
};
struct student stu1;

name of stu1 is given by -stu1.name same as stu1.rollno and stu1.marks.
++stu1.marks;
The dot operator is one of the highest precedence operators,its associativity  is from left to right. Hence it will take precedence over all othe unary,relational,logical,arithmetic and assignment operators. So above expression ,first stu1.marks will be accessed and then its value will be increased by 1.

Initialization Of Structure Variables - C Programming


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};

Declaring Structure Variables-C Programming


We can declare structure variable in two ways-
       1. With structure definition.
       2. Using the structure tag name.


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 structure template ,the  tagname is optional So we can declare them as -
struct {
char name[20];
int rollno;
float marks;
}stu1,stu2,stu3;

If we declare variable in this way, then we will not be able to declare other variables of this structure type anywhere else in the program nor can we send these structure variable to functions. It need arises to declare  a  variable of this type in the program the we will to write the whole template again.So tagname is optional but it is always better to specify a tagname for the structure.

2 . Using structure tagname

We can declare structure variable using structure tagname. 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 three members viz. name,rollno,marks.The compiler will reserve space for each variable sufficient to hold all the members. For example each structure variable will occupy 26 bytes.(char =20,int =2,float=4)

Note :: Size of data types depend on compiler. Do not confuse 

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.