1

Code

#include<stdio.h>
int main()
{
    char st[10],*sr[10];
    st="hiiii";
    sr="gdfsdfsd";
    printf("%s  %s",st,sr);

    return 0;
}

This program showing error as Lvalue required.

Is anyone can explain in detail or share me link where i can learn more thanks in advance?

EsmaeelE
  • 1,681
  • 5
  • 15
  • 22
ash
  • 11
  • 2
  • thankyou but i don't need alternates but i need to know why exactly the error is arising – ash Oct 07 '17 at 20:17
  • or `char st[]="hiiii"` – Jean-François Fabre Oct 07 '17 at 20:17
  • thankyou i know this will work but i want to know why the other one is not working i want the details of what causing this error – ash Oct 07 '17 at 20:18
  • `st` is not an `lvalue` because it cannot be assigned to directly. `st` is an array. You cannot assign to an array globally, only by subscripting with `[]` – Jean-François Fabre Oct 07 '17 at 20:20
  • 1
    The variable `st`'s value will always be the contents of the 10 character array. This cannot be changed. So putting `st` on the left side of an `=` makes no logical sense. It's value is, and always will be, that array. That cannot be changed. You can change the contents of the array, but not the value of `st`, which will always be the array, whatever its contents. – David Schwartz Oct 07 '17 at 20:36

2 Answers2

2

From What are rvalues, lvalues, xvalues, glvalues, and prvalues?:

An lvalue (so-called, historically, because lvalues could appear on the left-hand side of an assignment expression) designates a function or an object.

You're trying to assign something to an array (declared like type x[dimension];).

C language doesn't allow that. Hence, arrays cannot be lvalues, which explains the compiler error message.

Jean-François Fabre
  • 126,787
  • 22
  • 103
  • 165
1

You can't assign something to an array type, we can say that an array is not a left-value (the value that receives the assignment). There is a special case, you can assign to an array when you declare it, but not after that.

char array[] = "hello world"; // OK
array = "nop it doesn't work"; // KO

The only way to assign to an array after its declaration, is to copy the values into the array :

strcpy(array, "yeah it works");

Note that,

char array[];
array++;

obviously does not work, because it's the same thing as

array = array + 1

Always remember that arrays are not pointers.

Gam
  • 634
  • 1
  • 7
  • 16