C中的结构声明

Pav*_*mar 1 c struct typedef

是否遵循结构声明是对的.

typedef struct  { 
int roll; 
int  age ; 
} class[10];
Run Code Online (Sandbox Code Playgroud)

当我这样做时,编译器不会说任何错误.但是,当我指定class[0].age=10,
我得到错误.所以这里有class [0] struct变量或结构名称..

谢谢

Ker*_* SB 6

要定义一个类型 class,其是10层结构的阵列.要使用此类型,您必须实例化该类型的变量:

class x;
x[0].age = 10;
Run Code Online (Sandbox Code Playgroud)

也许稍微更简洁的方法是拥有两个独立的typedef:

typedef struct { int roll; int  age; } foo_unit;
typedef foo_unit foo_array[10];

foo_array x;     /* now an array of 10 foo_units. */
foo_unit  y[10]; /* same thing */
Run Code Online (Sandbox Code Playgroud)