dfighter said:
Keith said:
Franken Sense wrote:
If p and s are not integers in the following, why is their difference
guaranteed to be an integer? [...]
Because they are memory addresses, and those are integer type datas.
No, it's because that's how subtraction of two pointers is defined.
Pointers are not integers.
Not C integers, but integer datas, are they not?. I wasn't trying to
tell him that he can store them in integer variables in C. Sorry if I
was ambiguous.
Any C object's representation can be interpreted as an array of
unsigned char (bytes). All representations are composed of bits.
So in some sense, everything is an integer. But the mapping from
an object representation to integer values is not necessarily
meaningful.
For example, a typical floating-point representation consists of
a sign bit, an exponent, and a mantissa, each of which can be
interpreted as an integer value; the floating-point value can
be computed from these after some manipulation. (For example,
there's typically an implicit bias on the exponent value and an
implicit leading 1 on the mantissa.) But if you try to interpret
the entire floating-point object's representation as an integer,
you'll get gibberish. Well, you can reconstruct the floating-point
value from it if you know the implementation-specific details, but
there's no straighforward correspondence between the floating-point
value and the type-punned integer value.
The same applies to pointers, except that the standard says even less
about their innards. On many systems, a pointer representation is
a virtual (or perhaps physical) address that can be meaningfully
interpreted as an integer. But the standard carefully avoids
requiring this, or even suggesting it. The closest it comes is a
footnote in C99 6.3.2.3:
The mapping functions for converting a pointer to an integer
or an integer to a pointer are intended to be consistent with
the addressing structure of the execution environment.
but note that it says nothing about what that addressing structure is.
For example, there are real systems where a pointer is represented
as a pair of numbers, a segment number and an offset. On others,
different pointer types can have different representations; I've
worked on one where a word address is a machine address, but a byte
address shoves an offset into the upper 3 bits.
Pointers are not integers. Code that assumes that they are might
work on most systems that you're likely to encounter, but it will
break on others -- and it can often be re-written to use only the
properties of pointer arithmetic guaranteed by the standard.