问题描述
我试图理解在C函数声明中使用 static关键字作为数组索引的方法。
I'm trying to understand the use of the "static" keyword as an array index in a function declaration in C.
在阅读,我试图声明这样的功能,并有目的地向其传递太短的数组:
After reading this article, I tried to declare such a function, and purposefully passing it an array that is too short:
#include <stdio.h>
#include <stdlib.h>
void print_string10(char string10[static 10]) {
// Should trigger a warning if the argument is NULL or an array of less than 10 elements
printf("%s\n",string10);
}
int main(void) {
char short_string[] = "test";
print_string10(short_string); // should trigger a warning as the string is 5 long
return EXIT_SUCCESS;
}
使用 clang
进行编译如本文中的内容会触发警告,但 gcc -Wall -Werror
不会,它会编译并正常运行。
Compiling with clang
as in the article triggers the warning, but gcc -Wall -Werror
does not, it compiles and run fine.
我找不到解释,这是GCC忽略此警告的正常行为吗?
I couldn't find an explanation, is that a normal behaviour for GCC to omit this warning?
推荐答案
它看起来像这样是GCC中的错误。对于在编译时是否应报告此问题,似乎存在一些分歧。尽管如此,它仍被认为是错误,但似乎没有优先解决的问题。
It looks like this is a bug in GCC. It seems there were some disagreements regarding whether or not this should be reported at compile time. Nonetheless, it was accepted as a bug but there doesn't seem to be a priority on fixing it.
错误报告。评论9特别指出:
This is detailed in bug report 50584. Comment 9 in particular states:
尽管该功能是漂亮的还是丑陋的,GCC确实接受了
此代码,并且在可能的情况下,需要
警告未定义的行为。至此,证实。
Notwithstanding whether the feature is pretty or ugly, GCC does accept this code and warnings for undefined behavior, when possible, are desired. Thus, confirmed.
这并不意味着任何人都将致力于解决此问题。请使用
,如果您正在使用此功能,并且希望在GCC中看到此警告,请考虑使用
:
This does not mean anyone is going to work on fixing this. Please, if you are using this feature and would like to see this warning in GCC, please consider contributing it: https://gcc.gnu.org/wiki/GettingStarted#Basics:_Contributing_to_GCC_in_10_easy_steps
如果您开始进行此工作,最好在这里说。
If you start working on this, it would be good to say so here.
这篇关于GCC:函数参数中的静态数组索引不会触发任何警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!