Why sizeof(param_array) is the size of pointer?

Viewed 5349

I want to get the length of an array, say int array[] = {1, 2, 3, 4}. I used sizeof to do that.

int length(int array[])
{
     return sizeof(array) / sizeof(int);
}

int main()
{
    int array[] = {1, 2, 3, 4};
    printf("%d\n", length(array)); // print 1
    printf("%d\n", sizeof(array) / sizeof(int)); // print 4
}

So, why the sizeof(array) in function length returns the pointer size of array? But in function main, it works.

And, how should I modify the length function to get an array's length?

8 Answers
Related