This is one of the interview question, beginners could note, below is one of the way how we can find size of structure without using sizeof operator in C
It is explained with simple program as follows
struct test
{
int a;
char c;
double d;
};
int main()
{
struct test *p;
int size=0;
/*
* initialize struct pointer with
* zero after type casting it to of type (struct test *)
*/
p=(struct test *)0;
/* by adding one to p gives size of struct */
size =(int)(p+1);
printf("without using sizeof %d\n", size);
/* verify result with sizeof operator */
printf("with using sizeof %d\n", sizeof(struct test));
return 0;
}
same way we can find size of other variables as well :-)
Comments
Post a Comment