C语言中的数组和函数
如果有以下内容,我没有得到一些东西:
double average (double scores[]){
double sum = 0;
int n;
int nscores = sizeof(scores)/sizeof(double);
for (n=0 ;n<nscores ;++n){
sum+=scores [n];
return sum/nscores;
}
并且我向该函数发送一个像这样的数组:
double scores[3]={1,2,3};
为什么 sizeof(scores) 将为 0?
There i something i dont get, if have the following:
double average (double scores[]){
double sum = 0;
int n;
int nscores = sizeof(scores)/sizeof(double);
for (n=0 ;n<nscores ;++n){
sum+=scores [n];
return sum/nscores;
}
and i send this function an array like this:
double scores[3]={1,2,3};
why will sizeof(scores) will be 0?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在函数内部,
sizeof(scores)
相当于sizeof(double *)
:编译器此时无法判断数组有多大。它不会为零,但由于
sizeof(scores)
和sizeof(double)
都是整数类型表达式,sizeof (scores)/sizeof(double)
是整数除法,因此如果sizeof(scores)
sizeof(double)
,然后sizeof(scores)/sizeof(double) == 0。
sizeof(scores)
, inside the function, is equivalent tosizeof(double *)
: the compiler has no way to tell, at that point, how big the array was.It won't be zero, but because
sizeof(scores)
andsizeof(double)
are both integer-type expressions,sizeof(scores)/sizeof(double)
is integer division, so ifsizeof(scores) < sizeof(double)
, thensizeof(scores)/sizeof(double) == 0.
这是因为数组作为函数的参数被视为指针。例如,
... 相当于:
因此
sizeof(scores)/sizeof(double)
等于sizeof(double *)/sizeof(double)
,如果这些类型碰巧具有相同的大小,则结果为1
,但如果 double 的大小为 8 并且指针仅为 4,则结果为0
>。This is because array as a parameter of the function is treated as pointer. For example,
... is an equivalent to:
and so
sizeof(scores)/sizeof(double)
is equal tosizeof(double *)/sizeof(double)
, and if you happen to have those types to be of the same size, the result is1
, but if size of double is 8 and pointer is just 4, then result is0
.关于 C 语言中的数组,需要记住两件事:
除非它是
sizeof
或一元&
运算符的操作数,或者是用于初始化的字符串文字声明中的另一个数组,“T
的 N 元素数组”类型的表达式将替换为(“衰减到”)“指向T
的指针”类型的表达式" 其值是第一个元素的地址在函数参数声明的上下文中,
T a[]
和T a[N]
与T *a
相同; IOW,a
被声明为指向T
的指针,而不是数组。请注意,这仅适用于函数参数声明。当您从主函数调用
average(scores)
时,表达式scores
将被替换为另一个double *
类型的表达式,那么< code>average 接收的是一个指针值,而不是一个数组。因此,获取元素数量的sizeof
技巧在average
函数中不起作用。您必须将scores
中的元素数量作为单独的参数传递,如下所示:并将其调用为
Two things to remember about arrays in C:
Except when it is the operand of a
sizeof
or unary&
operator, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array ofT
" will be replaced with ("decay to") an expression of type "pointer toT
" whose value is the address of the first element of the array.In the context of a function parameter declaration,
T a[]
andT a[N]
are identical toT *a
; IOW,a
is declared as a pointer toT
, not as an array. Note that this is only true for function parameter declarations.When you call
average(scores)
from your main function, the expressionscores
will be replaced with another expression of typedouble *
, so whataverage
receives is a pointer value, not an array. Thus, thesizeof
trick to get the number of elements won't work within theaverage
function. You will have to pass the number of elements inscores
as a separate parameter, like so:and call it as
scores
是一个 double *const,因此sizeof(scores)
将是存储指针所需的大小scores
is a double *const, hencesizeof(scores)
will be size required to store a pointer