Assume that the following C declarations are in effect: int a[4] = {6, 4, 1, 2};
ID: 3815191 • Letter: A
Question
Assume that the following C declarations are in effect:
int a[4] = {6, 4, 1, 2};
int b[8] = {9, 8, 11, 10, 5, 7, 0, 3}; int *p = &a[1];
int *q = b;
int *r = b + 2;
Give the value of each of the following expressions. If an expression is illegal, give ILLEGAL as the answer. (Consider an expression to be illegal if it is rejected by a C compiler.) If an expression is legal but has an undefined value, give UNDEFINED as the answer.
a) *p
b) *q
c) *r
d) p + q
e) r - q
f) *(p + 1)
g) q[3]
h) b - a
Explanation / Answer
int a[4] = {6, 4, 1, 2};
int b[8] = {9, 8, 11, 10, 5, 7, 0, 3}; int *p = &a[1];
int *q = b;
int *r = b + 2;
a) *p => 4 => p pointing to second element of array a : a[1]
b) *q => 9 => q pointing to first element of array b: b[0], array name stores the address of first element
c) *r => 11 => r pointing to 3rd elemet of array b: r = b + 2 > adding 2 in base address
d) p + q => Illegal: invalid operands to binary expression ('int *' and 'int *')
e) r - q => 2 : number of blocks between r and q
f) *(p + 1) => 1 =>adding one in pointer points to next element, previously it was pointing to second element of a, now
it point to third element of array a
g) q[3] => 10 => q is pointing first element of b , now adding 3 in it, it points 4th element of b: q[3] = *(q+3)
h) b - a => // UNDEFINED
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.