gpt4 book ai didi

c - 表达式必须是可修改的左值

转载 作者:太空狗 更新时间:2023-10-29 16:23:28 32 4
gpt4 key购买 nike

我这里有 char text[60];

然后我在 if 中做:

if(number == 2)
text = "awesome";
else
text = "you fail";

它总是说expression 必须是可修改的左值。

最佳答案

lvalue 表示“左值”——它应该是可分配的。您不能更改 text 的值,因为它是一个数组,而不是一个指针。

要么将其声明为 char 指针(在这种情况下最好将其声明为 const char*):

const char *text;
if(number == 2)
text = "awesome";
else
text = "you fail";

或者使用strcpy:

char text[60];
if(number == 2)
strcpy(text, "awesome");
else
strcpy(text, "you fail");

关于c - 表达式必须是可修改的左值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6008733/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com