Give the minimum size of each of the following C data structures, assuming that
ID: 3815846 • Letter: G
Question
Give the minimum size of each of the following C data structures, assuming that char values occupy one byte, int and float values occupy four bytes, double values occupy eight bytes, and pointers occupy four bytes.
(a) char str[] = "Curly";
(b) double *a[4][4];
(c) char *str[3] = {"Moe", "Larry", "Curly"}; (Include the space occupied by the string literals in your answer.)
(d) union
{ int a;
char b;
float c[4];
} u;
(e) struct
{ int a;
char b;
float c[4];
} s;
(f) union
{ int a[3];
double b;
struct
{ float c;
char d[4];
} s;
} u;
(g) struct
{ float a;
union
{ double b[2];
int c; }
u;
char d;
} s;
Explanation / Answer
assuming that char values occupy one byte,
int and float values occupy four bytes,
double values occupy eight bytes,
and pointers occupy four bytes.
(a) char str[] = "Curly";
the length of the string is = 6 (including string end char '')
Therefor size is = 6Byte
(b) double *a[4][4];
here a is an array of double pointers
double * ((a[4])[4])
pointer will be having an address that is 4B in size.
Here size (16*4B) = 64Byte
(c) char *str[3] = {"Moe", "Larry", "Curly"}; (Include the space occupied by the string literals in your answer.)
here str is an array of character pointers. Each index of character stores the address of the string(char array).
Size = (3*4B) = 12Byte
(d) union
{ int a;
char b;
float c[4];
} u;
For the union maximum memory allocated will be equal to the data member with maximum size.
So here
size = MAX(4B,1B,16B) = 16Byte
(e) struct
{int a;
char b;
float c[4];
} s;
size will get calculated by summing all the element's size =
size = (4B+1B+(4*4B))=21Byte
(f) union
{ int a[3];
double b;
struct
{ float c;
char d[4];
} s;
} u;
size = MAX(12B,8B,(4B+4B)) = 12Byte
(g) struct
{float a;
union
{ double b[2];
int c; }
u;
char d;
} s;
SIZE = 4B+MAX(16B,4B)+1B = 4B+16B+1B =21Byte
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.