gpt4 book ai didi

c - 定义新数据类型时,数组类型的元素类型不完整

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:03 24 4
gpt4 key购买 nike

我有以下代码:

#include <stdio.h>

int main(void) {
typedef int new_type[];
new_type Number[8];
return 0;
}

编译器 (Gcc- 4.9.2) 显示错误:array type has incomplete element type
我有一些问题:
1. C90标准是否接受通过typedef定义new_type?在这种情况下,new_type 是一种数据类型,它定义了未定义数量的元素的数组 或指向 int 数组的指针 ?
2.如果我们保留 typedef int new_type[]; 行,我们如何定义类型为 new_type 的变量? 感谢您的帮助。

最佳答案

Is the definition of new_type through typedef accepted by C90 standard? In this case, new_type is a data type that defines an array of undefined number of element or a pointer to an array of int?

typedef 本身不是问题,因为您可以对不完整的类型进行 typedef (typedef struct A A_s;)。 new_type 确实和您想象的差不多。

If we keep the line typedef int new_type[];, how can we define a variable of type new_type?

对于变量定义,变量的类型必须是完整类型,所以:

typedef int new_type[];
new_type Number = { 1, 2, 3 };

将导致 Number 成为大小为 3 的 int 数组。但是,您尝试将 Number 定义为 int[] 数组> 大小为 8(int Number[8][];)。

但未指定大小的数组不能用于定义任何变量、数组或作为结构的成员1(不完整的类型不能用于这些目的)。


<子> 1. 除非是最后一个,灵活的数组成员是允许的(有很多限制)。

关于c - 定义新数据类型时,数组类型的元素类型不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42120069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com