On Sun, 10 Feb 2008 12:36:05 -0800, Keith Thompson wrote:
Harald van Dijk <tr*****@gmail.comwrites:
>On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
>>Dear all,
why the following program is giving o/p as
sizeof(struct emp) = 0
#include<stdio.h>
#include<stdlib.h>
struct emp
{ };
This isn't valid C. C requires at least one member in every structure.
That said, ...
>>i have seen this same code giving o/p as 1 in other compilers
...why would you expect any bytes to be required to store nothing?
Shouldn't you be asking why other compilers make sizeof(struct emp)
anything other than zero? Or do you already know why?
Padding.
In C, padding is not allowed at the immediate start of a structure.
More seriously, padding is used to ensure the structure's members are
properly aligned, and there's no problem with alignment of an empty
structure's members. There's also no problem with an array of zero-byte
objects. They all have the same address, so they are all identically
aligned.
Of course, the fact that they all have the same address is why a certain
other language places extra requirements on empty structures, but
extensions by any C compiler don't have to follow rules for other
languages.