What actually gets transferred to a function when an array is passed as an…
2025
What actually gets transferred to a function when an array is passed as an argument in C++?
- A.
The array is passed entirely by value.
- B.
A full duplicate of the array is created and used in the function.
- C.
Only the last element of the array is made available to the function.
- D.
The memory address of the array's first element is passed.
Attempted by 41 students.
Show answer & explanation
Correct answer: D
Because only the memory address of the first element (&arr[0]) is passed down to the function, two critical behaviors emerge in C++:
You must pass the size separately: Because the function only receives a raw pointer to the first element, it completely loses track of how long the array actually is. It cannot use
sizeof(myArr)to determine the length, which is why a second parameter (int size) is almost always required.Modifications affect the original array: Since the function holds the actual memory address, using subscript notation inside the function (like
myArr[i] = 100;) directly modifies the data cells of the original caller array via pointer arithmetic (*(myArr + i)).