C语言中的数组和函数

发布于 2025-01-03 00:43:15 字数 343 浏览 1 评论 0原文

如果有以下内容,我没有得到一些东西:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南街女流氓 2025-01-10 00:43:15

在函数内部,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 to sizeof(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) and sizeof(double) are both integer-type expressions, sizeof(scores)/sizeof(double) is integer division, so if sizeof(scores) < sizeof(double), then sizeof(scores)/sizeof(double) == 0.

沩ん囻菔务 2025-01-10 00:43:15

这是因为数组作为函数的参数被视为指针。例如,

double average (double scores[]);

... 相当于:

double average (double *scores);

因此 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,

double average (double scores[]);

... is an equivalent to:

double average (double *scores);

and so sizeof(scores)/sizeof(double) is equal to sizeof(double *)/sizeof(double), and if you happen to have those types to be of the same size, the result is 1, but if size of double is 8 and pointer is just 4, then result is 0.

深空失忆 2025-01-10 00:43:15

关于 C 语言中的数组,需要记住两件事:

  1. 除非它是 sizeof 或一元 & 运算符的操作数,或者是用于初始化的字符串文字声明中的另一个数组,“T 的 N 元素数组”类型的表达式将替换为(“衰减到”)“指向 T 的指针”类型的表达式" 其值是第一个元素的地址

  2. 在函数参数声明的上下文中,T a[]T a[N]T *a 相同; IOW,a 被声明为指向 T 的指针,而不是数组。请注意,这仅适用于函数参数声明。

当您从主函数调用 average(scores) 时,表达式 scores 将被替换为另一个 double * 类型的表达式,那么< code>average 接收的是一个指针值,而不是一个数组。因此,获取元素数量的 sizeof 技巧在 average 函数中不起作用。您必须将 scores 中的元素数量作为单独的参数传递,如下所示:

double average(double *scores, size_t count)
{
   ...
}

并将其调用为

average(scores, sizeof scores / sizeof *scores); // or pass a constant 3, or
                                                 // something similar.

Two things to remember about arrays in C:

  1. 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 of T" will be replaced with ("decay to") an expression of type "pointer to T" whose value is the address of the first element of the array.

  2. In the context of a function parameter declaration, T a[] and T a[N] are identical to T *a; IOW, a is declared as a pointer to T, not as an array. Note that this is only true for function parameter declarations.

When you call average(scores) from your main function, the expression scores will be replaced with another expression of type double *, so what average receives is a pointer value, not an array. Thus, the sizeof trick to get the number of elements won't work within the average function. You will have to pass the number of elements in scores as a separate parameter, like so:

double average(double *scores, size_t count)
{
   ...
}

and call it as

average(scores, sizeof scores / sizeof *scores); // or pass a constant 3, or
                                                 // something similar.
秋风の叶未落 2025-01-10 00:43:15

scores 是一个 double *const,因此 sizeof(scores) 将是存储指针所需的大小

scores is a double *const, hence sizeof(scores) will be size required to store a pointer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文