Computer languages are an abstraction over underlying data.
What is the difference between a
float and an
int? To the computer, it is just a collection of 32 bits (typically).
The difference is how they are treated.
How would you implement a
reference? A reference is a name that dynamically refers to another object. Hey, we've already got something similar: a pointer. A pointer references another object.
Remember, there is the high-level, abstract way of looking at things.
And there is the low-level, machine way of looking at things.
You are confusing the two.
A constant literal, like "10", is not guaranteed to be stored anywhere useful to you other than where it is used
[edit] and in the
context where it is used
[/edit]. As I already indicated:
The problem with literals is that they don't necessarily exist. The compiler is not only free to, but may need to, put them in the code in such a way that they cannot be accessed directly [as an object by itself]. |
For example, a MIPS processor packs both the instruction code and the values in the same machine word. You cannot reference a bitpacked field transparently (not without some significant overhead). |
All this is low-level thinking.
From the high-level perspective, just remember that unless your data is in a
const or a
variable, it doesn't exist as a first-class object (one you can do stuff to). Hence, something like
const int* ptr =
&10
;
is nonsense, because you are trying to get the address of something that is not guaranteed to exist: the literal value
10
.
Hope this helps.