mercredi 3 octobre 2018

Assigning 2D variable sized arrays

I'm trying to store a 2D array of variable length c-strings into a struct so I can transmit and rebuild it over a network socket.

The plan is to have rows and cols, which is in the header of the packet, help me read the variable size lens and arr that come after. I believe I must be syntactically writing the pointers incorrectly or there's some kind of aux pointer I need use when setting them into the struct.

struct STORAGE {
   int rows;     // hdr
   int cols;     // hdr
   int** lens;
   const char*** arr;
}

// code
int rows = 11;
int cols = 2;

int lens[rows][cols];
const char* arr[rows][cols];
// ... fill with strings ...
// ... along with lens ...

STORAGE store;
store.rows = rows;
store.cols = cols;
store.lens = lens;
store.arr = arr;

I get these errors when compiling this code:

error: invalid conversion from int to int** [-fpermissive]

error: cannot convert const char* [11][2] to `const char***' in assignment

I come from mostly a Java background, but I do understand how pointers work and such. The syntax of this one is just a little sideways for someone with my background (mostly write java/c++ and less c). Any suggestions?

Note: the reason why I'm not using more complex types like strings, maps, vectors, etc is that I need to transmit the structure over the network (ie pointers to the heap won't work if they have variable sizes). It must be low-level arrays unless someone can offer a better solution.

Aucun commentaire:

Enregistrer un commentaire