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

后端 未结 13 2154
[愿得一人]
[愿得一人] 2020-11-21 04:13

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

#include 

void PrintSize(int p_someArray[10]);

int main () {
           


        
13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 04:56

    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*)

提交回复
热议问题