abt Lvalue n Rvalue

An lvalue can be defined as:“An address in the memory that is the location of an object whose contents can be modified.”

On the other hand, an rvalue can be defined as:”A value that does not necessarily have any storage address. An lvalue can be converted to an rvalue, but _not_ the other way around.”

In the following example, observe the usage of j:

int k, j; /* line 1 */
j = 5; /* line 2*/
k = j; /* line 3*/

The compiler treats the variable j in two different ways: in the line 2, j is the address of the integer object; and in the line 3, it is the value of that object! Technically, j is an lvalue in the line 2; and, a rvalue in the line 3.

**************************************************************

An lvalue is an expression that could appear on the left-hand sign of an assignment (An object that has a location). An rvalue is any expression that has a value (and that can appear on the right-hand sign of an assignment).

The lvalue refers to the left-hand side of an assignment expression. It must always evaluate to a memory location. The rvalue represents the right-hand side of an assignment expression; it may have any meaningful combination of variables and constants.

Is an array an expression to which we can assign a value?

An lvalue was defined as an expression to which a value can be assigned. 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.
structure variable to another structure variable of the same type, such as this:
Thus, you can assign one

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;
...

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: