在c中初始化结构数组
Initializing array of structs in c
我已经为我初始化了一个包含三个项目的结构数组 it is showing 2!!!
#include <stdio.h>
typedef struct record {
int value;
char *name;
} record;
int main (void) {
record list[] = { (1, "one"), (2, "two"), (3, "three") };
int n = sizeof(list) / sizeof(record);
printf("list's length: %i \n", n);
return 0;
}
这里发生了什么?我疯了吗?
将初始化更改为:
record list[] = { {1, "one"}, {2, "two"}, {3, "three"} };
/* ^ ^ ^ ^ ^ ^ */
您使用 (...)
进行的初始化留下类似于 {"one", "two", "three"}
的效果,并创建一个包含元素 { {(int)"one", "two"}, {(int)"three", (char *)0} }
的结构数组
comma operator 在 C 中从左到右评估表达式并丢弃除最后一个以外的所有表达式。这就是 1
、2
和 3
被丢弃的原因。
您没有正确初始化 list
。 将初始化元素放在 ()
中将使编译器将 ,
视为 comma operator 而不是分隔符 。
您的编译器应该给出这些警告
[Warning] left-hand operand of comma expression has no effect [-Wunused-value]
[Warning] missing braces around initializer [-Wmissing-braces]
[Warning] (near initialization for 'list[0]') [-Wmissing-braces]
[Warning] initialization makes integer from pointer without a cast [enabled by default]
[Warning] (near initialization for 'list[0].value') [enabled by default]
[Warning] left-hand operand of comma expression has no effect [-Wunused-value]
[Warning] left-hand operand of comma expression has no effect [-Wunused-value]
[Warning] initialization makes integer from pointer without a cast [enabled by default]
[Warning] (near initialization for 'list[1].value') [enabled by default]
[Warning] missing initializer for field 'name' of 'record' [-Wmissing-field-initializers]
初始化应按
进行
record list[] = { {1, "one"}, {2, "two"}, {3, "three"} };
我已经为我初始化了一个包含三个项目的结构数组 it is showing 2!!!
#include <stdio.h>
typedef struct record {
int value;
char *name;
} record;
int main (void) {
record list[] = { (1, "one"), (2, "two"), (3, "three") };
int n = sizeof(list) / sizeof(record);
printf("list's length: %i \n", n);
return 0;
}
这里发生了什么?我疯了吗?
将初始化更改为:
record list[] = { {1, "one"}, {2, "two"}, {3, "three"} };
/* ^ ^ ^ ^ ^ ^ */
您使用 (...)
进行的初始化留下类似于 {"one", "two", "three"}
的效果,并创建一个包含元素 { {(int)"one", "two"}, {(int)"three", (char *)0} }
comma operator 在 C 中从左到右评估表达式并丢弃除最后一个以外的所有表达式。这就是 1
、2
和 3
被丢弃的原因。
您没有正确初始化 list
。 将初始化元素放在 ()
中将使编译器将 ,
视为 comma operator 而不是分隔符 。
您的编译器应该给出这些警告
[Warning] left-hand operand of comma expression has no effect [-Wunused-value]
[Warning] missing braces around initializer [-Wmissing-braces]
[Warning] (near initialization for 'list[0]') [-Wmissing-braces]
[Warning] initialization makes integer from pointer without a cast [enabled by default]
[Warning] (near initialization for 'list[0].value') [enabled by default]
[Warning] left-hand operand of comma expression has no effect [-Wunused-value]
[Warning] left-hand operand of comma expression has no effect [-Wunused-value]
[Warning] initialization makes integer from pointer without a cast [enabled by default]
[Warning] (near initialization for 'list[1].value') [enabled by default]
[Warning] missing initializer for field 'name' of 'record' [-Wmissing-field-initializers]
初始化应按
进行 record list[] = { {1, "one"}, {2, "two"}, {3, "three"} };