Passing array to function with pointer loses array size information!

Posted by Narek on Stack Overflow See other posts from Stack Overflow or by Narek
Published on 2010-05-01T09:38:58Z Indexed on 2010/05/01 9:47 UTC
Read the original article Hit count: 222

Filed under:
|
|
|

If I write

int main()
{
    int a[100] = {1,2,3,4,};
    cout<<sizeof(a)/sizeof(a[0])<<endl;
    return 0;
}

I get 400!

If I write

void func(int *a);

int main()
{
    int a[100] = {1,2,3,4,};
    func(a);
    return 0;
}

void func(int *a)
{
     cout<<sizeof(a)/sizeof(a[0])<<endl;
}

Then I get 400!

So why passing array to function with pointer loses array size information?

© Stack Overflow or respective owner

Related posts about c++

Related posts about pointers