In C, 2D array-of-strings indexing vs pointer arithmetic: what characters print? #include<stdio.h> int main() { static char mess[6][30] = { "Don't walk in front of me...", "I may not follow;", "Don't walk behind me...", "Just walk beside me...", "And be my friend." }; printf("%c, %c ", *(mess[2] + 9), ((mess + 2) + 9)); return 0; }

Difficulty: Easy

Correct Answer: k, k

Explanation:


Introduction / Context:
This question reinforces that array indexing and equivalent pointer arithmetic produce the same result. With an array of strings (2D char array), both expressions select the same character from the third string.



Given Data / Assumptions:

  • mess is a 6x30 char array with the third row (index 2) equal to "Don't walk behind me...".
  • Zero-based indexing applies.
  • We evaluate the character at offset 9 within row 2.


Concept / Approach:
For any array a, a[i] ≡ (a + i). Therefore, mess[2] (a char[30]) decays to a char pointing to the first character of the third string. Adding 9 moves to the 10th character; dereferencing yields that character. The pointer-form ((mess + 2) + 9) is the same computation spelled out explicitly.



Step-by-Step Solution:
Row 2 string: "Don't walk behind me...".Indexing characters (0-based): D(0) o(1) n(2) '(3) t(4) (space 5) w(6) a(7) l(8) k(9).Both expressions select index 9 → 'k'.printf prints: k, k.


Verification / Alternative check:
Replace the first with mess[2][9] and the second with *(&mess[2][0] + 9); both still refer to the same element.



Why Other Options Are Wrong:
(a), (c), and (d) pick other letters; (e) is incorrect since all indexing is within bounds.



Common Pitfalls:
Miscounting indexes due to spaces and punctuation; forgetting zero-based indexing.



Final Answer:
k, k

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion