错误:在赋值'const char [5]'到'char [10]'中的不兼容类型 [英] Error:incompatible types in assignment of 'const char[5]' to 'char[10]'
本文介绍了错误:在赋值'const char [5]'到'char [10]'中的不兼容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将c定义为
char c [] [10]
$ c $ p>
在函数定义中使用
c [i] =grey;
有什么问题?我在网上搜索,它显示相同的语法。
谢谢。
解决方案您不能在数组上使用赋值(
=
)。如果将c
更改为指针数组,这可能会有效,具体取决于您需要做什么。const char * c [20];
c [i] =gray;
或者如果声明的类型必须是数组数组,可以使用
strncpy
:char c [20] [10]
strncpy(c [i],grey,sizeof(c [i]));
I have defined c as
char c[][10]
in function definition and used it like
c[i]="gray";
Whats wrong? I searched on net, it shows the same syntax.
Thanks.
解决方案You cannot use assignment (
=
) on an array. If you changec
to an array of pointers, that might work, depending on what you need to do with it.const char *c[20]; c[i] = "gray";
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]'中的不兼容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文