soscpd said:
Hello James, Bartc, List
My question is - isn't the pointer the memory address? Just the memory
address, with nothing else?
The pointer value or object will never be 3 (sorry about "will not be
ever a value". What I mean is - the pointer value will never be 3, in
that case). It will be the address of a memory location. The memory
location will have the desired value (well... that depends of what you
are looking for).
[...]
The word "pointer", by itself, is ambiguous; it can refer either to a
pointer object or a pointer value. A pointer value is also called an
"address". (Well, mostly, I'm not sure that a null pointer value or
an indeterminate pointer value is an "address".)
You can also have a pointer type, such as int*.
To understand the distinction, let's look at integers. Again, the
word "integer" by itself could mean either an integer object, or an
integer value, or maybe an integer type. Concretely:
int x = 42;
x is an integer object. 42 is an integer value. int is an integer
type (one of several defined by the language). These are closely
related: int is the type of the integer object x, and of the integer
value 42, and 42 is the current value of the integer object x. An
integer *value* can be either the current value of an integer object,
or the result of evaluating an integer expression; the latter isn't
necessarily stored anywhere. If the value 42 is stored somewhere, it
will be represented as a sequence of bits, something like
0000000000101010 (assuming 16-bit ints).
It's the same thing with pointers:
int *p = &x; /* see above for the declaration of x */
p is a pointer object. &x is a pointer value (an address). int* is a
pointer type. int* is the type of the pointer object p, and of the
pointer value &x, and &x is the current value of the pointer object p.
Note that, unlike with integers, we don't have a literal notation for
pointer values, other than the special case of a null pointer
constant; I use the expression &x as a stand-in for that particular
value. (If you print the value using printf's "%p" format, you might
get something like "0x12345678", or "1234:5678", or "a suffusion of
yellow"; the latter is highly unlikely but not forbidden by the
standard. This is a printed representation of the pointer value; it
is not the pointer value itself.) If the value we're referring to as
``&x'' is stored somewhere, it will be represented as a sequence of
bits; the way it's represented is entirely system-specific. The
standard imposes some requirements on how integers are represented;
there are no such requirements for pointers.
And like any other value in C, a pointer value has a type as well as
just a raw value. The value that results from evaluating the
expression ``42'' has type int, and thus is distinct from the value
that results from evaluating the expression ``42.0'', which has type
double. Similarly, the value of ``&x'' is of type int*; it's not
*just* a machine address, it's an address with a particular type. The
value of ``(void*)&x'', though it refers to the same machine address,
has a different type. The type information isn't stored in memory
during program execution, it's tracked by the compiler during
compilation.