关于在函数中使用sizeof() [英] About using sizeof() in function
本文介绍了关于在函数中使用sizeof()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好!
最近,我学习C语言,遇到了问题.示例如下:
Hello!
Recently, I learn C language, I have a problem. The example as follow:
// function prototype
int numOfArray(int num[]);
int main()
{
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
printf("the number of array is: %d.\n", sizeof(a)/sizeof(a[0]));
printf("\n");
printf("after calling function.\n");
printf("the number of array is: %d.\n", numOfArray(a));
return 0;
}
int numOfArray(int num[])
{
return sizeof(num)/sizeof(num[0]);
}
结果是:数组数为:9.
调用函数后.
数组的数量为:1.
众所周知,数组是通过地址在函数中传输的.为什么两个结果不同?我希望有一个人可以帮助我.
The result is: the number of array is:9.
after calling function.
the number of array is:1.
As we know, array is transmitted in function through address. Why the two results are different? I hope someone can help me. Thanks.
推荐答案
sizeof
表达式的值是在编译期间确定的,而int numOfArray(int num[])
中的一个无法计算出原始大小.num
是那里的指针.
The value of asizeof
expression is determined during compiling period, and the one inint numOfArray(int num[])
can not figure out the original size.num
is pointer there.
这篇关于关于在函数中使用sizeof()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文