In C, consider the following program. Assume that the array arr begins at address 65486 in memory. int main(void) { int arr[] = {12, 14, 15, 23, 45}; printf("%u %u", arr, &arr); return 0; } What values will be printed, given that sizeof(int) is 2 bytes and the implementation prints addresses as unsigned integers?

Difficulty: Medium

Correct Answer: 65486 65486

Explanation:


Introduction / Context:
This question explores how array names and the address-of operator work in C when printing addresses. Although arr and &arr have different types, they both refer to the same starting memory location for the array. Understanding this distinction is important for pointer arithmetic and for avoiding confusion when passing arrays to functions.



Given Data / Assumptions:

  • The array arr has 5 int elements and begins at address 65486.
  • sizeof(int) is 2 bytes on the target system.
  • The program prints arr and &arr using the %u format specifier.
  • We are only concerned with the numeric address values printed, not with strict type safety of printf.


Concept / Approach:
In most expressions, the array name arr decays to a pointer to its first element, that is, a value of type int * pointing to arr[0]. The expression &arr, on the other hand, is the address of the entire array object and has type int (*)[5]. Despite the different types, both pointers contain the same memory address, namely the starting address of the array in memory. Differences appear only when we perform pointer arithmetic on these pointers, not when we simply print their raw address values.



Step-by-Step Solution:
Step 1: The starting address of arr[0] is given as 65486.Step 2: The expression arr in the printf call decays to &arr[0], which holds the address 65486.Step 3: The expression &arr is the address of the whole array object, which is also located at the same starting address 65486.Step 4: Therefore, both arguments to printf refer to the same underlying address.Step 5: When printed with %u, the output will be two identical numbers: 65486 65486.


Verification / Alternative check:
If we extended the example and printed arr + 1 and &arr + 1, we would see different values: arr + 1 would point to the second element and be 65488 (2 bytes ahead), while &arr + 1 would point one whole array size ahead, moving by 5 * sizeof(int). However, for the base addresses arr and &arr, both represent the starting memory location of the array, confirming that they print the same value.



Why Other Options Are Wrong:
Option B (65486 65496) would correspond to treating &arr as if it were already incremented by one array length, which does not happen here. Option C (65486 65488) incorrectly assumes that &arr is automatically offset by one element compared to arr. Option D is incorrect in practice because, although the types differ, many compilers allow this usage with a warning, and at run time the addresses are printed correctly.



Common Pitfalls:
Students often think that arr and &arr are completely different in both type and value. While they do have different types and behave differently under pointer arithmetic, they share the same starting address when not modified. Confusing their types can lead to subtle bugs, especially when passing &arr instead of arr to functions expecting int *.



Final Answer:
The program prints 65486 65486.


More Questions from Programming

Discussion & Comments

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