RAKHE wrote:
>
hi
struct name
{
char n2;
short n1;
long n3;
};
here actually sizeof(struct name) is 7;
but why it is printing it as 8 in gcc
Because sizeof(struct name) _is_ 8.
if any one find the answer could you explain how it is happening
It's called "padding" and it's in the FAQ somewhere. I'm sure
someone will post the section shortly.
Basically, your system probably requires (or at least "prefers") to
have shorts on even byte boundaries (and, perhaps, longs on 4-byte
boundaries). Therefore, the compiler adds a byte of padding between
n2 and n1. Use the offsetof() macro to demonstrate this, by printing
the offset of n2, n1, and n3.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody |
www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net |
www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>