Why isn't the size of an array parameter the same as within main?

Chris_45 picture Chris_45 · Dec 29, 2009 · Viewed 72.4k times · Source

Why isn't the size of an array sent as a parameter the same as within main?

#include <stdio.h>

void PrintSize(int p_someArray[10]);

int main () {
    int myArray[10];
    printf("%d\n", sizeof(myArray)); /* As expected, 40 */
    PrintSize(myArray);/* Prints 4, not 40 */
}

void PrintSize(int p_someArray[10]){
    printf("%d\n", sizeof(p_someArray));
}

Answer

Prasoon Saurav picture Prasoon Saurav · Dec 29, 2009

An array-type is implicitly converted into pointer type when you pass it in to a function.

So,

void PrintSize(int p_someArray[10]) {
    printf("%zu\n", sizeof(p_someArray));
}

and

void PrintSize(int *p_someArray) {
    printf("%zu\n", sizeof(p_someArray));
}

are equivalent. So what you get is the value of sizeof(int*)