我是 C 的新手。 假设我有一个 union 。
union DP
{
int c[3];
char a[3][4];
short b[2];
}point = {256,258,260};
printf("%i",point.a[0][1]);
printf("%i",point.a[1][2]);
printf("%i",point.a[2][0]);
为什么第一次打印给出 1,第二次打印为 0,第三次打印为 4?
最佳答案
您一次只能初始化union
的一个成员。 c
的元素(第一个成员)在此处初始化1。 a
和 b
的成员未初始化。访问未初始化的变量会调用未定义的行为。您可能会得到预期或意外的结果。
n1570:附件 J:J.2 未定义行为
The behavior is undefined in the following circumstances:
...
— An lvalue designating an object of automatic storage duration that could have been declared with the register storage class is used in a context that requires the value of the designated object, but the object is uninitialized (6.3.2.1).
1:其他成员可以使用C99的designated initializers来初始化
关于c - 关于打印阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25163238/