I was asked this as interview question. Couldn\'t answer.
Write a C program to find size of structure without using the
sizeof
operator.
For people that like C macro style coding, here is my take on this:
#define SIZE_OF_STRUCT(mystruct) \
({ struct nested_##mystruct { \
struct mystruct s; \
char end[0]; \
} __attribute__((packed)) var; \
var.end - (char *)&var; })
void main()
{
struct mystruct {
int c;
};
printf("size %d\n", SIZE_OF_STRUCT(mystruct));
}