arrays - 多维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
虽然合理,但确实需要在代码的其他部分进行大量额外工作。
解决方案
您可以使用指向数组的指针:
char (*args)[CONST_C] = strList[2];
现在代码:
puts(args[0]);
puts(args[1]);
puts(args[2]);
将产生:
and
found
none.
推荐阅读
- google-sheets - 对其他范围内所有出现的范围值求和
- android - 数组需要什么格式才能让微调器正确使用它?
- swift - 当 swift iOS 中出现意外值时,如何将 nil 设置为可编码枚举
- python - 通过 pip 安装模块后在 python 中“找不到模块”
- apache - Issue with apache proxy configuration for Web socket
- apache - Apache vhost - DirectoryIndex 不起作用
- jenkins - Jenkins - 将本地代码文件复制到 Jenkins 工作区所需的帮助
- react-native - setScreenName 上的 react-native 0.64.2 firebase 编译错误
- javascript - 如何通过事件委托收听不同的侦听器
- sharepoint - 当用户在 web 应用程序中进行搜索时,此页面不会与您共享