I have this code
struct Student { char name[48]; float grade; int marks[10,5]; char gender; }; Student s;
Now i need to get sizeof s
so i added
printf("%d",sizeof(s));
now that i got the compilation, the result shows 256
and its wrong because it must be 253
since the size
char name [48]; ----> 48
and
floating class; -----> 4
and
int tags [10,5]; ------> 200
and
char gender; -------> 1
therefore 48 + 4 + 200 + 1 = 253
so why does he tell me 256?
==================================
this part is written after I saw your answers
I found out that
Suppose I have this structure: struct {char a [3]; short int b; long int c; char d [3]; };
So..
+-------+-------+-------+ | a | +-------+-------+-------+ | b | +-------+-------+-------+-------+ | c | +-------+-------+-------+-------+ | d | +-------+-------+-------+
In the packed'' version, notice how it at least a little bit hard for you and me to see how the b and c fields wrap around? In a nutshell, it hard for the processor, too. Therefore, most compilers will panel packed'' version, notice how it at least a little bit hard for you and me to see how the b and c fields wrap around? In a nutshell, it hard for the processor, too. Therefore, most compilers will packed'' version, notice how it at least a little bit hard for you and me to see how the b and c fields wrap around? In a nutshell, it hard for the processor, too. Therefore, most compilers will packed'' version, notice how it at least a little bit hard for you and me to see how the b and c fields wrap around? In a nutshell, it hard for the processor, too. Therefore, most compilers will pad '' (as if with additional invisible fields):
+-------+-------+-------+-------+ | a | pad1 | +-------+-------+-------+-------+ | b | pad2 | +-------+-------+-------+-------+ | c | +-------+-------+-------+-------+ | d | pad3 | +-------+-------+-------+-------+
therefore, if Im having a maximum size is 200, if the padding will look like
48 + 152
4 + 196
200 + 0
1 + 199
to make them fit