The following
#include <iostream>
void printArraySize ( int * arr )
{
std::cout << sizeof(arr)/sizeof(int);
}
int main ()
{
int arr [] = {1, 2, 3, 69203};
printArraySize(arr);
return 0;
}
outputs 1
(Proof: http://codepad.org/yKG3mZIz). Explain this nonsense. Does an array forget its size once it enters a function? From what I understand, passing in arr
, a memory address, a number, just means making a copy of that number. So sizeof(arr)/sizeof(int)
should mean the same thing inside and outside of the function.