+ 6
Why is the size of s[2] 16 here?
#include <stdio.h> struct student {char *c;}; int main() { struct student s[2]; printf("%d",sizeof(s)); return 0; }
2 Antworten
+ 6
Hi there my friend, pardon me, but that was rather incorrect, the reason sizeof(s) returns 16 here is because the struct <student> contains a char pointer, and size of a pointer is generally 8 bytes (assuming in a 64bit system), thus an array of student structure having 2 elements is weighed for 2 * 8 (number of elements [2] multiplied by size of char pointer [8]).
The char type size is actually only 1 byte, check it with sizeof(char).
Regards,
+ 4
Because, the size of pointer is 8, we multiply it by the size of this array 2 , so: 8*2
edited, thanks to Ipang