简体   繁体   English

在结构错误内初始化2D数组

[英]Initializing a 2D array inside a struct error

When I initialize this array inside my struct. 当我在结构体中初始化此数组时。 I get the error message - syntax error : '{'. 我收到错误消息-语法错误:'{'。 Unexpected token(s) preceding '{'; '{'之前的意外令牌; skipping apparent function body. 跳过表观功能体。

int array[8][2] = {{3,6},{3,10},{3,14},{8,4}, {8,8},{8,12},{8,16},{12,2}};

I'm not sure what is wrong as I copied the syntax from my textbook. 我不确定从我的教科书中复制语法时出了什么问题。

Declaration is typedef struct _array *Array; 声明是typedef struct _array * Array;

You cannot initialize a variable inside a struct declaration, doesn't matter if an array or int. 您不能在struct声明中初始化变量,而与数组或int无关。 Yet, you can initialize the array in the struct initialization. 但是,您可以在结构初始化中初始化数组。

struct foo {
    int x;
    int array[8][2];
};

struct foo foovar = {1, {{3,6},{3,10},{3,14},{8,4}, {8,8},{8,12},{8,16},{12,2}}};

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

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