+ 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; }

21st Jan 2019, 7:34 AM
Zhenis Otarbay
Zhenis Otarbay - avatar
2 Réponses
+ 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,
21st Jan 2019, 10:05 AM
Ipang
+ 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
21st Jan 2019, 7:35 AM
Zhenis Otarbay
Zhenis Otarbay - avatar