Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following variable definitions: char a, *b, *c; int d[2], *e; int i

ID: 3768584 • Letter: C

Question

Consider the following variable definitions:

char a, *b, *c;

int d[2], *e;

int i, *j;

How many total bytes does this code allocate for variables? Assume a 32-bit representation for integer and pointer values.

a              sizeof(char)

b              sizeof(char *)

c              sizeof(char *)

d              2*sizeof(int)

e              sizeof(int *)

i               sizeof(int)

j               sizeof(int *)

Total number of bytes for each?

What is the output of the following piece of code? (Use the above variable definitions).

j = &i;

*j = 50;                                                  /* i = 50 */

e = &d[0];

*e = 20;                                                 /* d[0] = 20 */

e[1] = 30;                                                               /* d[1] = 30 */

j = &d[1];                                                               

*j = i+d[1];                                             /* d[1] = 80 */

printf(“d = [%d, %d]n”, d[0], d[1]);

char a, *b, *c;

int d[2], *e;

int i, *j;

Explanation / Answer

a              sizeof(char)

Total number of bytes = 1

b              sizeof(char *)

Total number of bytes = 2

c              sizeof(char *)

Total number of bytes = 2

d              2*sizeof(int)

Total number of bytes = 8

e              sizeof(int *)

Total number of bytes = 2

i               sizeof(int)

Total number of bytes = 2

j               sizeof(int *)

Total number of bytes = 2

Output of the following piece of code.

j = &i;

*j = 50;                                                  /* i = 50 */

e = &d[0];

*e = 20;                                                 /* d[0] = 20 */

e[1] = 30;                                                               /* d[1] = 30 */

j = &d[1];                                                               

*j = i+d[1];                                             /* d[1] = 80 */

printf(“d = [%d, %d]n”, d[0], d[1]);

output:

d=20,80