ProgrammingHard
int *arr = malloc(5 * sizeof *arr); for (int i = 0; i < 5; i++) arr[i] = i * i; int *q = arr + 2; printf("%d", q[1]); What is printed?
- A. 9
- B. 4
- C. 16
- D. 1
Correct answer: A. 9
q = arr + 2 points at arr[2], so q[1] is *(q + 1) = arr[3] = 3 * 3 = 9. 4 reads q[1] as arr[2], forgetting the offset adds to q, and 16 jumps one element too far to arr[4].
More C Programming questions
- struct N { int v; struct N next; } a = NULL; for (int i = 1; i <= 3; i++) { struct N n = malloc(sizeof n); n->v = i; n->next = a; a = n; }…
- Two pointers p and q point into the same block allocated with malloc(10 sizeof(int)). Which subtraction is defined?
- A linked list of length n is reversed in place with a loop. How much extra memory is used?
- unsigned int h = 0; for (const char p = "ab"; p; p++) h = h 31 + p; printf("%u", h); What is printed (ASCII, 32-bit unsigned)?
- unsigned int x = 0b11011010; printf("%u", (x >> 3) & 7); What is printed?
- With 32-bit unsigned arithmetic, what does (unsigned)-5 >> 1 print?