In the following C++ program, what is printed by cout when the array name is passed directly to the output stream? #include <iostream> using namespace std; int main() { int arr[] = {4, 5, 6, 7}; int *p = (arr + 1); cout << arr; return 0; }

Difficulty: Medium

Correct Answer: address of arr

Explanation:


Introduction / Context:
This question tests understanding of how arrays behave in C++ when used with streams and pointer decay. Instead of printing the contents of the array, passing the array name directly to cout interacts with the underlying pointer value. Recognizing this behaviour is important for debugging and for correctly printing arrays in C and C++ programs.


Given Data / Assumptions:

  • arr is declared as an array: int arr[] = {4, 5, 6, 7};
  • p is a pointer that points to arr + 1, but p is never used in the output.
  • cout << arr; is executed, and arr appears in an expression context.


Concept / Approach:
In most expressions, the name of an array decays to a pointer to its first element. For an int array, arr has type int[4], but in cout << arr it is converted to int*, pointing to the first element. The standard ostream insertion operator for int* prints the pointer value, usually represented as a memory address in implementation specific format. It does not print the first integer value 4 or any other element directly.


Step-by-Step Solution:
Step 1: Note that cout uses operator<< overloads to decide how to print values. Step 2: When arr is passed, it decays from int[4] to int*, which is a pointer type. Step 3: The ostream has an overload that accepts const void* or similar pointer types and prints them as addresses. Step 4: Therefore, cout outputs the address where the first element of arr resides, not the integer 4. Step 5: The exact address is implementation dependent, but conceptually the result is described as the address of arr.


Verification / Alternative check:
If you modify the code to cout << *arr; you will see the integer value 4 printed, because now you are dereferencing the pointer. Similarly, using a loop with cout << arr[i]; will print each element. Running the original code on any standard C++ compiler consistently prints a hexadecimal or integer address representation, confirming that the output corresponds to the address, not one of the numeric values 4, 5, 6, or 7.


Why Other Options Are Wrong:
Option A (4) and option B (5) are wrong because the code does not dereference the array or pointer. Option D (7) is also incorrect for the same reason. The program never uses p in the cout statement, so the pointer offset to arr + 1 has no effect on what is printed.


Common Pitfalls:
Many beginners assume that writing cout << arr; will print all elements of the array or at least the first element. This misunderstanding can lead to confusing outputs and debugging difficulty. The correct way is to use a loop or standard algorithms to print the contents. Remembering that array names decay to pointers and that ostream prints pointers as addresses is essential for correct mental models of C++ behaviour.


Final Answer:
The program prints the address of arr (that is, the address of the first element of the array), so the correct option is the description 'address of arr'.

More Questions from Technology

Discussion & Comments

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