Re: "Referenced type "



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
.



Relevant Pages

  • Re: The Advantage of Macros
    ... which is usually a single source file, can consist of external declarations ... void fx(int *x, int *y) { ... Also, if you think C is complicated, just take a look at languages like C++, ... C pushes to wrong programming styles. ...
    (alt.lang.asm)
  • Error in Building the Passthrough NDIS IM Driver from DDK
    ... 'NdisIMGetDeviceContext' undefined; assuming extern returning int ... 'NDIS_MINIPORT_CHARACTERISTICS': undeclared identifier ... '.MajorNdisVersion' must have struct/union type ...
    (microsoft.public.development.device.drivers)
  • Re: C objects
    ... >void foo{ ... >int i; ... An identifier is a lexical concept and is ... >talking about source code (the only part we have any real control ...
    (comp.lang.c)
  • Re: Another spinoza challenge
    ... extern int _MAX_POWER; ... It should be pretty clear that it is, reserved for the implementation because of the leading _, but it is a valid identifier. ... I think it perfectly reasonable to tell a beginner that it they are not allowed to use names which cannot on all implementations be used in a one line C file containing the single definition ... you're just asking what's a valid identifer - regardless of context. ...
    (comp.lang.c)
  • Re: What is the difference between :foo and "foo" ?
    ... > experience in languages with symbols tend to talk more about ... while programmers ... I'm somewhat new to ruby, and for me it has made most sense to do an ri ... surprized to find out that any identifier (class, method, variable, ...
    (comp.lang.ruby)