Reputation: 633
Based on corrections of annotations to the standard.
3.14 An object is either a variable or a constant that resides at a physical memory address.
- In C, a constant does not reside in memory, (except for some string literals) and so is not an object.
It makes sense to me that a const will not have an actual memory location (except possibly for string literals) and that it depends on the compiler, which will most likely replace all references to it with the literal value. That being said, how is the following possible?
const int * foo;
This declares a pointer to a const int
. However, constants don't have an address, so what does this really mean? You can't have a pointer to a literal value that only exists at compile time.
Upvotes: 2
Views: 187
Reputation: 123468
In your example, foo
is not a constant, it is a const-qualified object:
6.7.3 Type qualifiers
Syntax
1 type-qualifier:
const
restrict
volatile
...
3 The properties associated with qualified types are meaningful only for expressions that are lvalues.114)
...
114) The implementation may place aconst
object that is notvolatile
in a read-only region of storage. Moreover, the implementation need not allocate storage for such an object if its address is never used.
Upvotes: 3
Reputation: 183888
A const
variable is not a constant. A constant is a literal value or an expression composed of literal values, like 3+5/2.4
. Such constants indeed don't reside in memory, the compiler inserts the literal at the appropriate places.
Upvotes: 4