C FAQ 6.7


C

ccwork

Hi,
In C FAQ, question 6.7:

How can an array be an lvalue, if you can't assign to it?
-------------------------------------------------------------
The ANSI C Standard defines a ``modifiable lvalue,'' which an array is
not.

References: ANSI Sec. 3.2.2.1
ISO Sec. 6.2.2.1
Rationale Sec. 3.2.2.1
H&S Sec. 7.1 p. 179

I don't understand why array is lvalue. To me the answer is saying
that array is NOT lvalue.
 
Ad

Advertisements

R

Richard Bos

ccwork said:
In C FAQ, question 6.7:

How can an array be an lvalue, if you can't assign to it?
-------------------------------------------------------------
The ANSI C Standard defines a ``modifiable lvalue,'' which an array is
not.

References: ANSI Sec. 3.2.2.1
ISO Sec. 6.2.2.1
Rationale Sec. 3.2.2.1
H&S Sec. 7.1 p. 179

I don't understand why array is lvalue. To me the answer is saying
that array is NOT lvalue.

That would've made the most sense, yes, but it isn't how the Standard
does it. I don't know why not, but I suspect historical reasons.

Richard
 
L

Lawrence Kirby

Hi,
In C FAQ, question 6.7:

How can an array be an lvalue, if you can't assign to it?

The standdard defines the term "lvalue" to meet its own purposes. In
simple terms the stabdard uses the term lcalue to describe an expression
that designates an object. A footnote suggests that it could be thought of
as a "locator value". This is similar to but not the same as the classical
meaning of something that can appear on the left on an assignment. An
obvious example is

const int x = 1;

Here x is an lvalue, it designates an object, &x makes sense and so on,
but x cannot appear on the left hand side of an assignment.

"Modifiable lvalue" is a close match to the classic meaning of "lvalue".
References: ANSI Sec. 3.2.2.1
ISO Sec. 6.2.2.1
Rationale Sec. 3.2.2.1
H&S Sec. 7.1 p. 179

I don't understand why array is lvalue. To me the answer is saying
that array is NOT lvalue.

It is an lvalue because it designates an object. However it is not a
modifiable lvalue because it cannot be used directly to modify the object.

In the standard the concept of lvalue is used in the specification of
things like what a valid operand of & is. The definition of lvalue in the
standard is tailored to meet that need, although the definition in C99 is
badly messed up.

Lawrence
 
A

Andrey Tarasevich

ccwork said:
...
How can an array be an lvalue, if you can't assign to it?
...
I don't understand why array is lvalue. To me the answer is saying
that array is NOT lvalue.
...

In C language the term "lvalue" has absolutely nothing to do with being
or not being assignable. Lvalue is any value of object type (or non-void
incomplete type). The idea of lvalue in C is that any object that has a
location in memory is "lvalue" ("l" stands for "location"). Whether you
can assign to it or not is completely irrelevant.
 
K

Keith Thompson

Andrey Tarasevich said:
In C language the term "lvalue" has absolutely nothing to do with being
or not being assignable.

Well, yes and no. Only lvalues are assignable, but not all lvalues
are assignable.
Lvalue is any value of object type (or non-void
incomplete type). The idea of lvalue in C is that any object that has a
location in memory is "lvalue" ("l" stands for "location"). Whether you
can assign to it or not is completely irrelevant.

42 is a value of object type (specifically int), but it's not an
lvalue (the definition in the C99 standard implies that it is, but
that's not the intent). An lvalue is an expression that designates
an object, at least potentially (e.g., given
char *ptr;
the expression *ptr is an lvalue, even if ptr==NULL).

The 'l' in lvalue originally stood for "left", as in the left hand
side of an assignment; an rvalue was an expression that could appear
on the right hand side of an assignment. By the time C was
standardized, the meaning of "lvalue" came to include things that
can't be on the LHS of an assignment, and the term "rvalue" was
dropped. "location value" is a good mnenomic for the current meaning
of the term, but it's a later invention.
 
A

Andrey Tarasevich

Keith said:
Well, yes and no. Only lvalues are assignable, but not all lvalues
are assignable.

The OP's question seems to suggest that he views assignability as a
_defining_ property of an lvalue. I'm just saying that it is not. In the
current C the situation is quite the opposite: "lvalue-ness" is one of
the required pre-conditions of "assignability".
 
Ad

Advertisements

K

Keith Thompson

Andrey Tarasevich said:
The OP's question seems to suggest that he views assignability as a
_defining_ property of an lvalue. I'm just saying that it is not. In the
current C the situation is quite the opposite: "lvalue-ness" is one of
the required pre-conditions of "assignability".

Ok, but that's not what you said.
 
T

Tim Rentsch

ccwork said:
Hi,
In C FAQ, question 6.7:

How can an array be an lvalue, if you can't assign to it?
-------------------------------------------------------------
The ANSI C Standard defines a ``modifiable lvalue,'' which an array is
not.

References: ANSI Sec. 3.2.2.1
ISO Sec. 6.2.2.1
Rationale Sec. 3.2.2.1
H&S Sec. 7.1 p. 179

I don't understand why array is lvalue. To me the answer is saying
that array is NOT lvalue.

The address ('&') operator can be used with an array for its operand.
An array needs to be an lvalue because the address operator requires
that.

int a[10];
int *p = a;
int (*pa)[10] = &a;
 
Ad

Advertisements

P

pete

Tim Rentsch wrote:
The address ('&') operator can be used with an array for its operand.
An array needs to be an lvalue because the address operator requires
that.

The address operator is also defined for function type operands.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top