[EMAIL PROTECTED] wrote:


#include <stdio.h>
struct verify {
char initials[2];
int birthdate;
};
int main(void)
{
struct verify holes;
printf ("%d\n", sizeof(holes.initials[0]));
printf ("%d\n", sizeof(holes.initials));
printf ("%d\n", sizeof(holes.birthdate));
printf ("%d\n", sizeof(holes));
return 0;
}
Given that the word-byte
In 16-bit computer = 2 bytes word the output is,
1
2
2
4
in  32-bit computer = 4 bytes word the output is,
1
2
4
8



k, I am a newb, so someone plz quickly explain to me why the variable 'initial' takes 2 bytes, 'birthdate' takes 4 bytes but the struct which is 2+4 = 6 bytes
takes 8 bytes?


You refer to 32-bit intel computer in the above illustration.

Our compiler allocates,
the start of struct at the word alignment and memory must
be contiguous.

So, 2 characters = initials is started at the beginning of a word
byte and our word byte = 4 bytes. But only the first 2 bytes
are taken because we have only two characters. The last two
bytes in the 'word' are empty.

When the compiler continued on to allocate memory for the
next object in the struct, which in this case is int, this is
allocate memory beginning at the next word byte by skipping
the two empty bytes.

So,

2 char + 2 empty  + 4 bytes = 8 bytes.

The is the struct itself and has 8 bytes.

--
O Plameras
http://www.acay.com.au/~oscarp/tutor

--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to