为什么数组参数的大小与 main 中的大小不同? [英] Why isn't the size of an array parameter the same as within main?
本文介绍了为什么数组参数的大小与 main 中的大小不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么作为参数发送的数组大小与 main 中的数组大小不同?
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));
}
推荐答案
当您将数组类型传递给函数时,它会隐式转换为指针类型.
An array-type is implicitly converted into pointer type when you pass it in to a function.
所以,
void PrintSize(int p_someArray[10]) {
printf("%zu\n", sizeof(p_someArray));
}
和
void PrintSize(int *p_someArray) {
printf("%zu\n", sizeof(p_someArray));
}
是等价的.所以你得到的是 sizeof(int*)
are equivalent. So what you get is the value of sizeof(int*)
这篇关于为什么数组参数的大小与 main 中的大小不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文