When we pass an array as an argument to function,
2014
When we pass an array as an argument to a function, what actually gets passed ?
- A.
Address of the array
- B.
Values of the elements of the array
- C.
Base address of the array
- D.
Number of elements of the array
Attempted by 547 students.
Show answer & explanation
Correct answer: C
Answer: Base address of the array
Explanation: When an array is used as an argument in most languages like C, the array name decays to a pointer to its first element. The function therefore receives the base address (a pointer) that refers to the first element of the array, not a copy of all element values or the array length.
What is passed: a pointer to the first element (the base address).
What is not passed automatically: a copy of all element values and the number of elements.
Example: For int arr[], calling a function like void f(int *p) with f(arr) makes p point to arr[0]. Inside f you can access elements via p, but the function does not know the array length unless you pass it separately.
Note on types: Taking the address of the whole array (for example using &array) yields a pointer with a different type (pointer-to-array), even though the numeric address is the same. The common behavior when passing arrays as function arguments is the decay to pointer-to-element.