0

I have declared these two structures:

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;

In main function I want to create array of arrays of the type 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;

What I want to do is that first element of table_arrays points to tables.radiation_insolation. The next element to tables.radiation_radiation and so on. I know the way I do it now is wrong, so I ask you how to do it correctly?

5
  • You have type-names (aliases) that can be used just like other native types like int. Now, can you define an array of int? Then you can define an array of TABLE_ as well. Commented Aug 8, 2017 at 17:07
  • "I want to create array of arrays if the type TABLE_" did you mean "of the type TABLES"? Commented Aug 8, 2017 at 17:07
  • @Weather Vane: see: TABLE_ radiation_insolation[7] is array of type TABLE_. So I need array of arrays of type TABLE_ Commented Aug 8, 2017 at 17:23
  • Then why did you mention TABLES at all? Commented Aug 8, 2017 at 17:27
  • TABLES tables is a wrapper for the arrays. The code is there for context purposes. Commented Aug 8, 2017 at 17:42

2 Answers 2

2

If you declare a pointer to an array, you need to allocate space for it (e.g. with malloc()) before you can assign to the elements. But there's no need to use a pointer, just declare an array and initialize it as you want.

TABLE_ *table_arrays[] = {
    tables.radiation_insolation,
    tables.radiation_radiation,
    tables.winds,
    tables.pressure,
    tables.humidity,
    tables.temperature
}
Sign up to request clarification or add additional context in comments.

Comments

0

Correctly this should be like this:

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.