43

I have here char text[60];

Then I do in an if:

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

and it always said expression must be a modifiable L-value.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
Mysterigs
  • 443
  • 1
  • 4
  • 8

1 Answers1

63

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");
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
MByD
  • 135,866
  • 28
  • 264
  • 277