如何在C中创建结构数组?

如何在C中创建结构数组?,c,arrays,struct,C,Arrays,Struct,我宣布这两种结构: typedef struct { int skip_lines; int num; int i; char filename[70]; char main_directory[16]; char submain_directory[100]; } TABLE_; typedef struct { TABLE_ radiation_insolation[7]; TABLE_ radiation_radiation

我宣布这两种结构:

typedef struct {
    int skip_lines;
    int num;
    int i;
    char filename[70];
    char main_directory[16];
    char submain_directory[100];
} TABLE_;

typedef struct {
    TABLE_ radiation_insolation[7];
    TABLE_ radiation_radiation[5];
    TABLE_ winds[9];
    TABLE_ pressure[1];
    TABLE_ humidity[1];
    TABLE_ temperature[4];
} TABLES;

在main函数中,我想创建TABLE类型的数组数组

TABLES tables; // this will be filled with data later

// Now my pseudo-code:
TABLE_ ** table_arrays;
table_arrays[0] = tables.radiation_insolation;
table_arrays[1] = tables.radiation_radiation;
table_arrays[2] = tables.winds;
table_arrays[3] = tables.pressure;
table_arrays[4] = tables.humidity;
table_arrays[5] = tables.temperature;

我想做的是table_数组的第一个元素指向tables.radiation_insolation。下一个元素是tables.radiation\u radiation等等。我知道我现在这样做的方式是错误的,所以我问你如何正确地执行它?

如果你声明一个指向数组的指针,你需要为它分配空间(例如使用
malloc()
),然后才能分配给元素。但是不需要使用指针,只需声明一个数组并根据需要初始化它

TABLE_ *table_arrays[] = {
    tables.radiation_insolation,
    tables.radiation_radiation,
    tables.winds,
    tables.pressure,
    tables.humidity,
    tables.temperature
}

如果声明指向数组的指针,则需要为其分配空间(例如,使用
malloc()
),然后才能分配给元素。但是不需要使用指针,只需声明一个数组并根据需要初始化它

TABLE_ *table_arrays[] = {
    tables.radiation_insolation,
    tables.radiation_radiation,
    tables.winds,
    tables.pressure,
    tables.humidity,
    tables.temperature
}

正确地说,应该是这样的:

void initiate(TABLES * tables){
  tables->radiation_insolation[0].num = 7; // initiate here
}
void processTables(TABLES * tables, TABLE_ * table_arrays[]){
   // do something with the pointer to TABLES or with pointer to TABLES_ array
}

TABLES tables;
TABLE_ * table_arrays[6];
initiate(&tables);
table_arrays[0] = tables.radiation_insolation;
table_arrays[1] = tables.radiation_radiation;
table_arrays[2] = tables.winds;
table_arrays[3] = tables.pressure;
table_arrays[4] = tables.humidity;
table_arrays[5] = tables.temperature;
processTables(&tables, table_arrays);

正确地说,应该是这样的:

void initiate(TABLES * tables){
  tables->radiation_insolation[0].num = 7; // initiate here
}
void processTables(TABLES * tables, TABLE_ * table_arrays[]){
   // do something with the pointer to TABLES or with pointer to TABLES_ array
}

TABLES tables;
TABLE_ * table_arrays[6];
initiate(&tables);
table_arrays[0] = tables.radiation_insolation;
table_arrays[1] = tables.radiation_radiation;
table_arrays[2] = tables.winds;
table_arrays[3] = tables.pressure;
table_arrays[4] = tables.humidity;
table_arrays[5] = tables.temperature;
processTables(&tables, table_arrays);

您有类型名(别名),可以像其他本机类型一样使用,如
int
。现在,您可以定义
int
的数组了吗?然后你也可以定义一个
表的数组。“如果类型
,我想创建一个数组”你的意思是“类型
”?@Weather Vane:see:
表uu辐射照射[7]
是表的数组。所以我需要TABLE_类型的数组数组,那你为什么要提到
TABLES
TABLES TABLES
是数组的包装器。代码用于上下文目的。您可以使用类型名(别名),就像使用其他本机类型一样,如
int
。现在,您可以定义
int
的数组了吗?然后你也可以定义一个
表的数组。“如果类型
,我想创建一个数组”你的意思是“类型
”?@Weather Vane:see:
表uu辐射照射[7]
是表的数组。所以我需要TABLE_类型的数组数组,那你为什么要提到
TABLES
TABLES TABLES
是数组的包装器。代码用于上下文目的。