为什么要在函数头中声明 C 数组参数的大小?
谁能告诉我为什么我应该在函数头中指定 C 数组参数的大小?例如:
void foo (int iz[6]) { iz[42] = 43; }
With:
int is[2] = {1,2,3};
我们得到一个有用的错误。也许它有助于评论/文档?
Can anyone enlighten me as to why I should bother to specify the size of a C array argument in a function header? For example:
void foo (int iz[6]) { iz[42] = 43; }
With:
int is[2] = {1,2,3};
we get a useful error. Perhaps it helps with commenting/documentation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(5)
这样做的唯一有意义的原因是出于文档目的 - 告诉未来的用户函数期望接收至少包含这么多元素的数组。但即便如此,这也是一个惯例——您必须事先与其他用户达成一致。无论如何,语言(编译器)都会忽略该大小。您的函数声明相当于 void foo(int iz[])
和 void foo(int *iz)
。
使其对编译器有意义的唯一方法是将其声明为,
void foo (int iz[static 6])
这向编译器承诺该数组将至少有 6 个元素,这意味着编译器将能够使用该假设来优化该代码。此外,如果您确实想采用上述约定,那么专门使用 static
声明数组参数大小更有意义,因为该语言显式定义了此构造的语义。
我不清楚你所说的“我们得到一个有用的错误”是什么意思。该代码
int is[2] = {1,2,3};
is[42] = 42;
不包含任何约束违规。它会产生未定义的行为,但不需要在编译期间产生诊断消息。换句话说,不,我们不会从中得到任何“有用的错误”。
恕我直言,在现代编译器版本(如现代 gcc)中,以前的答案现在是错误的。
事实上,在这些编译器的现代版本中,编译器使用函数头中指定的 arraw 大小来发出非常有用的警告。
例子 :
在 gcc 12 上,如果您指定 :
int f(char string[32])
并使用 : 调用该函数,
int main{
char paul[6]="paul"
f(paul);
}
编译器将发出警告,表明您可以在 f 中执行使用 32 个字符的操作,但您仅指向 6 个字节的内存空间。
所以:在现代编译器中,它有助于精确你的意图,并让编译器对你的意图和你编码的内容进行额外的检查。
完整示例:
#include <stdio.h>
void f(char string[32])
{
printf("%s\n",string);
}
int main(){
char paul[5]="paul";
f(paul);
}
将通过:
testi.c: In function ‘main’:
testi.c:12:9: warning: ‘f’ accessing 32 bytes in a region of size 5 [-Wstringop-overflow=]
12 | f(paul);
| ^~~~~~~
testi.c:12:9: note: referencing argument 1 of type ‘char[32]’
testi.c:5:6: note: in a call to function ‘f’
5 | void f(char string[32])
| ^
对 gcc testi.c -o my_executable 进行简单调用
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
,你不应该。当您尝试将数组传递给函数时,真正传递的是指向数组开头的指针。由于函数接收的将是一个指针,因此最好将其编写为明确的:
然后,由于现在很清楚该函数没有得到大小的线索,因此将其添加为单独的参数:
IMO, you shouldn't. When you try to pass an array to a function, what's really passed is a pointer to the beginning of the array. Since what the function receives will be a pointer, it's better to write it to make that explicit:
Then, since it's now clear that the function has been given no clue of the size, add that as a separate parameter: