Re: "Referenced type "
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Thu, 19 Feb 2009 18:49:25 -0500
Bartc wrote:
I used to have a problem understanding the difference between, say, an int
object and an int value, especially in languages having proper constants.
Imagine C has a 'constant' prefix, similar to an enum:
int x;
constant int y=42;
x=41;
Both x and y are of type int, yet &x is allowed but not &y.
The answer is that a variable such as x above is actually a 2-level value,
while the constant exists at a single level. The language hides this
difference. [...]
Let's take very small steps, because when we make grand
soaring leaps we sometimes jump to unintended destinations.
First step: `x' is an identifier, a lexical construct in
the C source code.
Second step: By virtue of the declaration `int x;', the
identifier `x' is associated with ("bound to") a variable of
type int. This association holds for a particular lexical
region of the C source (the "scope" of the declaration); in
other regions the identifier `x' may be associated with
something else entirely, or with nothing at all.
Third step: Within the declaration's scope, the appearance
of the identifier `x' in an expression causes the program to
make use of some feature of its associated variable when the
expression is evaluated. There are four cases:
3.1: In a "value" context like `x + 1' or `while (x)' the
program retrieves a representation from the variable
associated with the identifier `x', interprets that
representation as a value of int type, and uses that value
and type to evaluate the expression.
3.2: In an "assignment" context like `x = 42' the program
converts a value to a representation which it stores in
the variable associated with the identifier `x'.
(Note that cases 3.1 and 3.2 can overlap, as in `x++'.)
3.3: In the context `sizeof x', the program replaces the
entire sub-expression with a value of type size_t denoting
the number of bytes the variable associated with the
identifier `x' would occupy if it were stored in memory.
3.4: In the context `&x', the program replaces the entire
sub-expression with a value of type int* that points to the
variable associated with the identifer `x'.
There's no "two-level value" at work here, and nothing is
hidden. It's just the same convention that many programming
languages use: Some uses of a variable's identifier call for a
value to be read from the variable, some cause a value to be
stored, and some are used in connection with other attributes
of the variable. A programming language might use different
notations for these different uses -- observe Knuth's LOC(X)
and CONTENTS(X), for example -- but C uses a more streamlined
convention. By contrast many assembly languages lack notation
for "value stored in identifier's variable," and the identifier
represents only and always a reference to that storage slot
(so you write `ld r0,x' and `st r0,x' explicitly).
Of course, in ordinary conversation we tend not to keep
saying "the variable associated with the identifier `x'," but
simply refer to `x' and trust the listener to understand what's
meant. The listener usually does, in much the same way the C
compiler does: Context determines what aspect of `x' we're
talking about.
--
Eric.Sosman@xxxxxxx
.
- References:
- "Referenced type "
- From: saurabh29789
- Re: "Referenced type "
- From: Eric Sosman
- Re: "Referenced type "
- From: blargg
- Re: "Referenced type "
- From: Keith Thompson
- Re: "Referenced type "
- From: Bartc
- "Referenced type "
- Prev by Date: Re: Remebering the order of precedence
- Next by Date: Re: Switch for floating !!
- Previous by thread: Re: "Referenced type "
- Next by thread: Re: "Referenced type "
- Index(es):
Relevant Pages
|