相关疑难解决方法(0)

我可以将长数组初始化减少到C中的短初始化列表吗?

我有一些看起来像这样的C99代码:

struct foo {
    char* buf;
    size_t buf_sz;
};

struct foo list[3];
list[0].buf = "The first entry";
list[0].buf_sz = strlen(list[0].buf);
list[1].buf = "The second entry";
list[1].buf_sz = strlen(list[1].buf);
list[2].buf = "The third entry";
list[2].buf_sz = strlen(list[2].buf);
Run Code Online (Sandbox Code Playgroud)

有没有在初始化列表中写这个的简写方法?这样安全吗?

struct foo list[] = {
    { "The first entry", strlen(list[0].buf) },
    { "The second entry", strlen(list[1].buf) },
    { "The third entry", strlen(list[2].buf) }
};
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
152
查看次数

标签 统计

c ×1