简体   繁体   English

包含二维数组类型的C结构定义

[英]C struct definition containing 2-dimensional array type

I want to include a variable length two-dimensional array pointer as part of a struct, but the c99 compiler gives me the following error on the Array anArray line: "flexible array member in otherwise empty struct". 我想将可变长度的二维数组指针作为结构的一部分,但是c99编译器在Array anArray行上给了我以下错误:“灵活的数组成员,否则为空结构”。

typedef const int Array[][2];

typedef struct {
    Array anArray;
} StructType;

Array myArray = {{1,2},{3,4},{5,6}};

StructType myStruct = { myArray };

I would appreciate any insight into this problem and the solution. 我将对这个问题和解决方案有任何见解。 I will eventually be adding other components to StructType . 我最终将向StructType添加其他组件。

The structure needs to know the size of the array, and here you're missing one dimension. 结构需要知道数组的大小,在这里您缺少一维。 If declaration and initialization were done at the same time, you could eg 如果声明和初始化同时完成,则可以例如

int Array[][2] = {{1,2},{3,4},{5,6}};

then the compiler makes the space for the array and sets the value (otherwise that would have to be at run time, using dynamic allocation). 然后,编译器将为数组留出空间并设置值(否则将必须在运行时使用动态分配)。

Secondly, in C unfortunately that practical way of initializing an array is not possible 其次,不幸的是在C语言中,初始化数组的实际方法是不可能的

 StructType myStruct = { myArray };

That would have to be done at runtime (and C would have some trouble performing that kind of assignment in the case of dynamic allocation, for instance, since there is no mechanism to keep objects sizes up to date). 这必须在运行时完成(例如,在动态分配的情况下,C会在执行这种分配时遇到一些麻烦,因为没有机制可以使对象的大小保持最新)。

What you can do, though, is setting the size of the missing dimension, and copy the array thanks to the memory function memcpy 但是,您可以做的是设置缺少的维的大小,并memcpy内存函数memcpy复制数组。

typedef const int Array[3][2];

typedef struct {
    Array anArray;
} StructType;

int main(int argc, char **argv) {

    Array myArray = {{1,2},{3,4},{5,6}};

    StructType myStruct;
    memcpy(myStruct.anArray,myArray,sizeof(myArray));

You can also do the structure declaration and initialization this way 您也可以通过这种方式进行结构声明和初始化

StructType myStruct = { {{1,2},{3,4},{5,6}} };

and in C99, even 甚至在C99中

StructType myStruct = { .anArray = {{1,2},{3,4},{5,6}} };

Thanks for the comments. 感谢您的评论。 I changed the code to the following, and now it compiles. 我将代码更改为以下代码,现在可以编译了。

typedef const int Array[][2];

typedef const struct {
    int something;
    Array *pAnArray;
} StructType;

Array myArray = {{1,2},{3,4},{5,6}};

StructType myStruct = { 0, &myArray };

What you did without knowing is creating a struct with a flexible array member. 您不知道做的是使用灵活的数组成员创建结构。 Usually in C all members of structs and unions need to have complete types, ie their length must be exactly known at compile time. 通常在C语言中,所有结构体和联合体成员都必须具有完整的类型,即它们的长度必须在编译时准确知道。 However, there is an exception for the last member that does not need to but instead becomes the "flexible array member" . 但是,最后一个成员有一个例外,它不需要但可以成为“灵活数组成员” The problem in your case is that you do not have any other members in the struct, and this is not legal (I can't come up with a convincing rationale why this is atm). 您遇到的问题是,结构中没有其他成员,这是不合法的(我无法提出令人信服的理由来解释为什么这是atm)。 Storing a pointer in the struct is of course not equal to storing the complete array but it is probably more like what you want (at least as a beginner). 在结构体中存储指针当然不等于存储完整的数组,但它可能更像您想要的(至少是初学者)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM