R@M3$H.NBlog

Size of Structure without using sizeof()

01 February, 2013 - 1 min read

#define SIZEOF(X) (unsigned int)( (char *)(&X+1)-(char *)(&X) )

struct sample *ptr;
ptr=(struct sample*)0;
printf("%d",++ptr);

p1 = (unsigned char*)(ptr);

p2 = (unsigned char*)(++ptr);

size = p2 - p1;

Can also find out by declaring an Array of structure and finding the difference between addresses of the first and second element in the array.

struct sample s[2] ;

int s1 = (int) &s[0] ;

int s2 = (int) &s[1] ;

printf ( "%d\n", s2 - s1 ) ;