首页 > 解决方案 > 多维char数组和指针赋值

问题描述

假设我有一个 3 维 char 数组

char strList[CONST_A][CONST_B][CONST_C];

和一个数组指针(在评论指出错误后更改):

char * args[CONST_C];

我想选择其中的一部分strList并使其args成为那个价值。例如,如果strList表示类似

{{"Your", "question", "is", "ready", "to", "publish!"},
 {"Our", "automated", "system", "checked", "for", "ways", "to", "improve", "your", "question"},
 {"and", "found", "none."}}

我希望 args 是

{"and", "found", "none."}

我该怎么做?

我尝试使用args = strlist[someIndex];但出现错误提示incompatible types when assigning to type ‘char *[100]’ from type ‘char (*)[100]’ strcpy似乎也失败了(可能是由于args没有分配足够的空间?),我应该怎么做才能正确分配args

编辑:args已在分配之前使用,因此更改的类型args虽然合理,但确实需要在代码的其他部分进行大量额外工作。

标签: arrayscstringchar

解决方案


您可以使用指向数组的指针:

char (*args)[CONST_C] = strList[2];

现在代码:

    puts(args[0]);
    puts(args[1]);
    puts(args[2]);

将产生:

and
found
none.

推荐阅读