Question 16: Predict the output. (Considering int for 4 bytes)
void display(int data[])
{
std::cout<<"\nSize of array when passed as argument is : "<<sizeof(data);
}
int main()
{
int data[] = { 1, 2, 3, 4, 5, 6};
std::cout<<"\nSize of array is : "<<sizeof(data);
printSizeOfArray(data);
}
Options:
(a) Size of array is : 24
Size of array when passed as argument is : 4
(b) Size of array is : 24
Size of array when passed as argument is : 24
(c) Compile Time error.
(d) None of the above.
Show Solution
Solution: a
Explanation : The value will be passed as pointer instead of whole data.
In C and C++ it is NOT possible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address. In practice this has almost the same effect and it is a much faster and more efficient operation.
To be safe, you can pass the array size or put const qualifier before the pointer to make sure the callee won't change it.