c - 在 c99 模式之外循环静态分配的数组?

标签 c c99 loops

这是引用发布在以下位置的解决方案: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/

相关文章:

c++ - 使用 libpng 将 OpenGL 屏幕像素保存为 PNG

c - 哪些可用的 GNU C 扩展在 C99 中实现起来并非易事?

计算 C99 中两个日期字符串之间的工作日数

c - C 中的整数类型

c - 在 C 中格式化非常大的数字

c - C 中使用 void 指针的通用堆栈不适用于字符串

c - 如何在 C 中对 argv 的元素进行排序?

php - 如何将两个数组组合成一个循环?

java - 输入并指定要打印的字符以及每行要打印的字符数

java - 求二维数组中某一列的平均值并将结果放入另一个矩阵中