Addresses of argv elements (pointers) and their contiguity: predict subsequent &argv[i] values.\n\n/* myprog.c /\n#include <stdio.h>\n#include <stdlib.h>\n\nint main(int argc, char argv)\n{\n int i;\n for (i = 1; i <= 3; i++)\n printf("%u\n", (unsigned)&argv[i]);\n return 0;\n}\n\nGiven that the first printed address is 65517, find the next two values.

Difficulty: Medium

Correct Answer: 65519 65521

Explanation:


Introduction / Context:
This problem focuses on how argv is laid out in memory: it is an array of pointers (char). Taking &argv[i] yields the address of the i-th pointer object within that array. Consecutive pointer objects are stored contiguously, so their addresses differ by sizeof(char).


Given Data / Assumptions:

  • Architecture consistent with 16-bit model where sizeof(char) = 2 bytes for the displayed addresses.
  • First printed value (address of argv[1]) is 65517.
  • The code prints &argv[1], then &argv[2], then &argv[3].


Concept / Approach:
Since argv is an array of pointers, &argv[i+1] = &argv[i] + 1 * sizeof(char*). With sizeof(char*) = 2, each subsequent address increases by 2.


Step-by-Step Solution:

&argv[1] = 65517 (given).&argv[2] = 65517 + 2 = 65519.&argv[3] = 65519 + 2 = 65521.


Verification / Alternative check:
On systems with 4-byte pointers, the increments would be +4 instead; the question fixes the first value to imply a 2-byte stride.


Why Other Options Are Wrong:

  • 65525 65531 / 65521 65525: Use a 4-byte stride inconsistent with the given context.
  • 65517 65517: Implies no increment, which is incorrect for an array of distinct elements.


Common Pitfalls:
Confusing &argv[i] (address of the pointer element) with argv[i] (address of the string). These are different layers.


Final Answer:
65519 65521

Discussion & Comments

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