Array is an lvalue or not?

An lvalue was defined as an expression to which a value can be assigned. Is an array an expression to which we can assign a value? The answer to this question is no, because an array is composed of several separate array elements that cannot be treated as a whole for assignment purposes. The following statement is therefore illegal: int x[5], y[5]; x = y; Additionally, you might want to copy the whole array all at once. You can do so using a library function such as the memcpy() function, which is shown here: memcpy(x, y, sizeof(y));  It should be noted here that unlike arrays, structures can be treated as lvalues. Thus, you can assign one structure variable to another structure variable of the same type, such as this: typedef struct t_name { char last_name[25]; char first_name[15]; char middle_init[2]; } NAME; ... NAME my_name, your_name; ... your_name = my_name; ...

Showing Answers 1 - 21 of 21 Answers

Guest

  • Nov 28th, 2005
 

Basically lvalue is a storage location where data can be stored/ assigned and moreover it is not an expression. rvalue is an expresion which has a value. all lvalues can be used as rvalues. But all rvalues can't be used as lvalues. int a[100];a is always an rvalue, but a[i] can be lvalue as well as rvalue

Deepak kumar Prasad

  • Aug 30th, 2006
 

The rvalue is the data value of the variable, that is, what information it contains. The "r" in rvalue can be thought of as "read" value. A variable also has an associated lvalue. The "l" in lvalue can be though of as location, meaning that a variable has a location that data or information can be put into. This is contrasted with a constant. A constant has some data value, that is an rvalue. But, it cannot be written to. It does not have an lvalue.

  Was this answer useful?  Yes

i_spy

  • Dec 24th, 2007
 

I have a doubt. How about this statement:

int a[] = {"1", "4", "6", "3"};

It is definition and initialization but is also assignment for whole of array a.
Isn's array a being treated here as an lvalue?

  Was this answer useful?  Yes

Array is not an lvalue..
Eg.
int arr[5] = {"1","2",.......};

here arr[5] has  memory address and now we are assinging values to this.
if we write arr[5]; in any function then it will not show any error, mean array does required lvalue unlike structure

  Was this answer useful?  Yes

marchinram

  • Aug 19th, 2010
 

An array is an unmodifiable lvalue, not an rvalue.  rvalues cannot be used with the & operator and an array can be used with & operator, however this is pretty useless as it returns the same address as not using the & operator.

  Was this answer useful?  Yes

 Per the C Language Standard (hoping the posting software doesn't munge the following so that it's completely unreadable):

---------------------
6.3.2.1 Lvalues, arrays, and function designators

1  An lvalue is an expression with an object type or an incomplete type other than void; 53)  if an lvalue does not designate an object when it is evaluated, the behavior is undefined.  When an object is said to have a particular type, the type is specified by the lvalue used to designate the object.  A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or a union, does not have any member (including, recursively, any member of all contained aggregates or unions) with a const-qualified type.
...
53) The name "lvalue" originally comes from the assignment expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue.  It is perhaps better considered as representing an object "locator value".  What is sometimes called an "rvalue" is in this International Standard described as the "value of an expression". 
---------------------------

So, in short: arrays are objects, as they take up space in memory.  Array expressions are lvalues as defined by the language specification; however, they are not modifiable lvalues, and so cannot be the target of an assignment. 

  Was this answer useful?  Yes

adarshjoshi

  • May 12th, 2011
 

An object is a region of storage that can be examined and stored into. An lvalue is an expression that refers to such an object. An lvalue does not necessarily permit modification of the object it designates. For example, a const object is an lvalue that cannot be modified. The term modifiable lvalue is used to emphasize that the lvalue allows the designated object to be changed as well as examined. The following object types are lvalues, but not modifiable lvalues:

  • An array type
  • An incomplete type
  • A const-qualified type
  • A structure or union type with one of its members qualified as a const type

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions