c/c++: ‘sizeof’ on array function parameter ‘b’ will return size of ‘int *’ Wsizeof-array-argument

这个警告的含义,是说sizeof如果作用在函数参数上,而且这个参数的类型是一个数组,就会返回此类型的指针大小。

从标准文档上看,有说,数组类型不能作为函数的返回值,而且数组类型的参数,会转换为指针类型的参数。
ISO/IEC 9899:1999 (E)
6.7.5.3 Function declarators (including prototypes) Constraints
A function declarator shall not specify a return type that is a function type or an array type.
A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many
elements as specified by the size expression.

通过上面的限制,当一个数组作为参数进到函数之后,就调整为了指针。和外围定义的数组的sizeof 用法不一样,导致初学者的困惑。

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=6940 ;; 这个bug添加了这个警告。
https://ask.csdn.net/questions/8002757 ;; 相关的一个问题

猜你喜欢

转载自blog.csdn.net/qq_36428903/article/details/133277589
今日推荐