Possible Duplicate:
Sizeof array passed as parameter
I am being stupid with this sizeof operator in c++, do you have any idea why it is 4 and 12 ?
void function (int arg[]) {
cout<<sizeof(arg)<<endl; // 4
}
int main ()
{
int array[] = {1, 2, 3};
cout<<sizeof array<<endl; // 12
function (array);
return 0;
}
In
main, the namearrayis an array so you get the size in bytes of the array withsizeof. However, an array decays to a pointer when passed to a function, so you getsizeof(int*)inside the function.Be aware that taking an argument in the form of
T arg[]is exactly the same as taking the argument asT* arg. So your function is the exact equivalent of