Error:incompatible types in assignment of #39;const char[5]#39; to #39;char[10]#39;(错误:将“const char[5]分配给“char[10]时的类型不兼容)
问题描述
我已将 c 定义为
char c[][10]
在函数定义中并像 c[i]="gray";
怎么了?我在网上搜索,它显示相同的语法.
Whats wrong? I searched on net, it shows the same syntax.
谢谢.
推荐答案
您不能对数组使用赋值 (=).如果您将 c 更改为指针数组,这可能会起作用,具体取决于您需要使用它做什么.
You cannot use assignment (=) on an array. If you change c to an array of pointers, that might work, depending on what you need to do with it.
const char *c[20];
c[i] = "gray";
或者如果声明的类型必须是数组数组,你可以使用 strncpy:
Or if the declared type must be array of arrays, you could use strncpy:
char c[20][10];
strncpy(c[i], "gray", sizeof(c[i]));
这篇关于错误:将“const char[5]"分配给“char[10]"时的类型不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:错误:将“const char[5]"分配给“char[10]"时的类型不兼容
基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 这个宏可以转换成函数吗? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
