sizeof(array) returns different values in main() and user defined functions

Feb 11, 2010 at 1:29pm
Hello, everyone. I found sizeof(array) only works correctly in main() function.
Could you please tell me why?

-------------------------------------------------
#include <iostream>
using namespace std;

int f(const int array[]){
cout << sizeof(array) << endl;
//cout << array[index] << endl; //1
}

int main(){
int array[5] = {3,4,9,1,5};
cout << sizeof(array) << endl;
f(array);
return 0;
}

The output is:
20
4
------------------------------------------------
but if I print the whole array in f() (remove comment at 1),
the array content can be still printed out.

Can anyone explain the reason behind it?

Thank you very much.
Last edited on Feb 11, 2010 at 1:40pm
Feb 11, 2010 at 2:18pm
Answered in general c++ forum.
Feb 12, 2010 at 7:26am
Thanks for telling me. :-)
Topic archived. No new replies allowed.