Friday, May 4, 2012

structure padding and pointer inside structure

#include <stdio.h>

struct pint{
int *pintx;
char *pch;
long *plong;
}point;
struct pint1{
char pch;
int pintx;
long plong;

}point1;
void main()
{

    printf("\n---------Hello-------------\n");
    printf("\nsizeof structure point %dbytes--",sizeof(point));
    printf("\nsizeof point.pch %dbytes--",sizeof(point.pch));
    printf("\nsizeof point.pintx %dbytes--",sizeof(point.pintx));
    printf("\nsizeof point.plong %dbytes--",sizeof(point.plong));
    printf("\nsizeof structure point1 %dbytes--",sizeof(point1));
    printf("\nsizeof point1.pch %dbytes--",sizeof(point1.pch));
    printf("\nsizeof point1.pintx%dbytes--",sizeof(point1.pintx));
    printf("\nsizeof point1.plong%dbytes--",sizeof(point1.plong));

}


Here see the output
Structure point  having Different Data types but the size of the each
variable is same even they are different  data types, why because all
 the variabled are pointer variable .size is common to hold the address.

In Structure point 1  sum of size of the individual variable is 9.But in
actual it shows 12 how ?.  Because of structure padding .it occupy
three more bytes.



No comments:

Post a Comment