ç多维字符串数组 [英] C multidimensional array of strings
问题描述
我声明了一个字符串数组很干脆,硬codeD,但它一直给我的数组类型具有不完整的元素类型
错误。
我想这事做,每个数组的长度,但我不知道如何解决它没有设置一个固定长度的字符串。
分配的char [] [2] [] = {//错误使用或不使用2
{值1,值2},
{值3,值4}
};
这语法无效。如果你想有一个真正的多维阵列,所有的尺寸必须指定,除了第一个。 (编译器必须知道内部数组有多大,以便执行地址计算的外部尺寸。)
试试这个:
为const char *分配[] [2] = {
{值1,值2},
{值3,值4}
};
它声明为const char *
的二维数组
请注意,如果你想字符串,你可以的写的到,那么上面的方法将无法工作。
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.
这篇关于ç多维字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!