我可以先声明数组,后来给它的价值? [英] Can I declare an array first and give its value later?
本文介绍了我可以先声明数组,后来给它的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想这样做,但得到了一个错误。为什么我不能这样做呢?
INT的main()
{
焦甜[5];
甜=KOVA;
的printf(我最喜爱的甜品为%s \\ n,甜);返回0;
}
解决方案
不,你不能。很简单,因为数组名的不可修改的左值。不能被用作任何前pression左操作数。因此,你不能保持它放在 =
前pression的左侧。
不过,如果你想在字符串 KOVA
复制到阵列甜蜜[]
,您可以使用的strcpy()
是这样的。
的strcpy(甜,KOVA);
I tried doing this but got an error. Why can't I do this?
int main()
{
char sweet[5];
sweet = "kova";
printf("My favorite sweet is %s\n", sweet);
return 0;
}
解决方案
Nope, you cant. Simply because array names are non-modifiable l-value. Which cannot be used as left operand in any expression. Therefore, you cannot keep it on the left side of a =
expression.
However, If you want to copy the string kova
to the array sweet[]
, you can use strcpy()
like this.
strcpy(sweet,"kova");
这篇关于我可以先声明数组,后来给它的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文