我有一些看起来像这样的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