Expression must be a modifiable L-value

后端 未结 1 1841
我在风中等你
我在风中等你 2020-12-13 06:30

I have here char text[60];

Then I do in an if:

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


        
相关标签:
1条回答
  • 2020-12-13 06:55

    lvalue means "left value" -- it should be assignable. You cannot change the value of text since it is an array, not a pointer.

    Either declare it as char pointer (in this case it's better to declare it as const char*):

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

    Or use strcpy:

    char text[60];
    if(number == 2) 
        strcpy(text, "awesome"); 
    else 
        strcpy(text, "you fail");
    
    0 讨论(0)
提交回复
热议问题