简体   繁体   中英

C multidimensional array of strings

I'm declaring an array of strings very simply, hard-coded, but it keeps giving me the array type has incomplete element type error.

I guess this has something to do with the length of each array but I don't know how to fix it without setting a fixed length for the strings.

char allocate[][2][] = {  // Error with or without the 2
    {"value1","value2"},
    {"value3","value4"}
};

That syntax isn't valid. If you want a true multi-dimensional array, all the dimensions must be specified, except the first one. (The compiler must know how big the "inner" arrays are in order to perform address calculation for the outer dimensions.)

Try this instead:

const char *allocate[][2] = {
    {"value1","value2"},
    {"value3","value4"}
};

It declares a 2D array of const char * .

Note that if you want strings that you can write to, then the above approach will not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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