这是引用发布在以下位置的解决方案:Looping a fixed size array without defining its size in C
这是我的示例代码:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
static const char *foo[] = {
"this is a test",
"hello world",
"goodbye world",
"123",
NULL
};
for (char *it = foo[0]; it != NULL; it++) {
printf ("str %s\n", it);
}
return 0;
}
尝试编译得到:
gcc -o vararray vararray.c
vararray.c: In function ‘main’:
vararray.c:14: warning: initialization discards qualifiers from pointer target type
vararray.c:14: error: ‘for’ loop initial declaration used outside C99 mode
最佳答案
除了 for 循环中的初始化之外,您还在错误的地方递增。我认为这就是您的意思(请注意,我不完全是 C 大师):
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
static const char *foo[] = {
"this is a test",
"hello world",
"goodbye world",
"123",
NULL
};
const char **it;
for (it=foo; *it != NULL; it++) {
printf ("str %s\n", *it);
}
return 0;
}
关于c - 在 c99 模式之外循环静态分配的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1969706/