开发者

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 elem开发者_如何学Goent 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜