【发布时间】:2021-07-30 12:23:25
【问题描述】:
我遇到了一个有趣的情况——将一个不完整的数组大小 typedef 补全到一个已知的数组大小,例如:
#include <stdio.h>
typedef struct hi_
{
int a, s;
} hi[];
int main() {
hi there = { {3,7}, {1,5} };
hi there2 = { {11,17} };
printf("«1» 1/a: %i, 1/s: %i\n2/a:%i 2/s:%i\n", there[0].a, there[0].s, there[1].a, there[1].s);
printf("«2» 1/a: %i, 1/s: %i\n", there2[0].a, there2[0].s);
// Output:
// «1» 1/a: 3, 1/s: 7
// 2/a:1 2/s:5
// «2» 1/a: 11, 1/s: 17
}
我有点惊讶,因为在this answer 中非常强烈地支持这种类型“无法完成”。那么,这是为什么以及如何工作的呢?
【问题讨论】:
-
打个比方:
int a[]= {1,2,3};声明了一个大小为 3 个整数的数组并对其进行初始化。您的代码也是如此。类型是完整的,只是类型定义中没有给出它的大小。
标签: arrays c incomplete-type completion