Keith Thompson said:
Ben Bacarisse said:
Keith Thompson said:
[...]
What about the following...
http://67.40.109.61/torek/c/expr.html
And I quote..
"Array objects have a special, fundamental rule in C. This rule is
essentially arbitrary, and simply must be memorized. It falls out
from a key fact: C does not have array values. (There is one
exception to this, which I will save for later.) C does have array
objects -- just the values are missing. For instance, int a[5];
declares an ordinary array containing five ints. Logically, the
'value' of this array ought to be the five int values stored in that
array -- but it is not. Instead, the 'value' of the array is a
pointer to the first element of that array. "
The point being that, if I understand correctly, C does not have
'array of integers'. Instead, at least according to the former
comp.lang.c regular on here, the language has 'array objects'.
I find the quoted text misleading at best.
C does have array values; it just doesn't let you manipulate them
directly in most cases. The standard's definition of the word
"value" (C99 3.17) is "precise meaning of the contents of an object
when interpreted as having a specific type". Nothing in that
definition excludes arrays.
That definition is effectively useless. Functions, for example, are
said to return values. What object is being interpreted to get the
value returned by afunction? In the expression '1 + 1.0' something (it
is a value?) of type int is converted to a value of type double by the
usual arithmetic conversions, but there are no objects involved here.
The same goes for (int)1.0.
The problem with the definition is that it's incomplete (like a
number of other definitions in the standard). What it describes
is certainly an *example* of a value, but surely the evaluation of
an expression also results in a *value*. [snip..snip..snip]
The only consistent interpretation I can come up with is:
The Standard's definition of "value" is incomplete.
Any object in which something valid has been stored (handwaving meant to
exclude trap representations and perhaps indeterminate values) has a
"value", which is the "precise meaning of the contents of an object
when interpreted as having a specific type". [snip elaboration]
Let me suggest a different and IMO more useful way of thinking
about this.
A value is the /meaning/ of an object (considered as a particular
type). The key word here is /meaning/ - a 'value' is an
abstraction, an idea, not something that can be held in a
program, but a point in an abstract value space that follows
certain rules (eg, mathematical addition, etc).
It's true that bit patterns stored in objects are used to
represent values (again, under a particuilar type). However, the
value itself -- the "meaning" -- is something that does not
depend on any object for its existence: an unchanging, abstract
idea (such as, for example, the number 3).
When the Standard says an expression yields a certain value, what
that means is the program produces something whose "meaning" is
the same if the abstract value were stored into an object that
has the same type as that of the value. (All values in C
programs are effectively indexed by what type they are.) More
concretely, an expression like
21 + 21
yields the same value (ie, the same meaning) as is held by
the variable 'x' (an object) after
int x = 42;
Viewed from this perspective, what is produced by evaluating an
expression has the same meaning as some bit pattern stored in an
object. Because it has the same meaning, it is the same value.
To say that another way, objects do hold patterns of bits that
represent values, but values don't depend on objects for their
existence -- if something has the same meaning as a certain bit
pattern would if held in an object, then that is the same value
as the object would have, whether an object is present or not.
(As always, subject to the necessary qualifications about types.)
I agree that the text of the Standard takes certain linguistic
liberties -- eg, saying an expression yields a value (which is
quite nonsensical, whatever expressions produce it certainly
isn't a 'meaning') -- that sometimes are frustrating or
confusing. However, I don't think the problem in this case is
that the definition is incomplete; rather, there is some
looseness in the language that describes how expressions work,
glossing over the distinction between abstract values and what
is produced in an actual program that represents such values.
Because the distinction usually isn't important, the Standard
largely ignores it. Is that a big problem? Personally I would
say no: most people understand it without having to think too
hard, and that's probably more important than trying to be
completely precisely accurate if that comes with some loss in
readability or ease of understanding.
I would of course support a proposed change that results in text
that is more precise and equally readable. I just haven't seen
one yet.