Take the 2-minute tour ×

Text books say that & (addressof) operator doesn't apply to cannot be applied to expressions,
constants, or register variables.
Does constants mean only literals like 'A', '7' etc or variables declared with const keyword as well?
I think this mean only literals since following code compiles:-

int main()
const int i=10;
const int *ip;

ip = &i;


share|improve this question
add comment

3 Answers

Unary operator & in C can be applied to any lvalue. A const-qualified object is an lvalue, which means that unary & can be applied to it.

The term "constant" in C indeed means only literal constants, like 2, for example. A const-qualified object is not a "constant" in C terminology.

share|improve this answer
Any lvalue "that designates an object that is not a bit-field and is not declared with the register storage-class specifier", to be precise. –  caf Nov 29 '09 at 12:13
add comment

No -- it can be applied to a variable that's qualified as const. Note, however, that doing so (generally) means that the compiler has to actually assign that variable an address -- if you only use it in ways that don't need an address, a const variable often won't need to be assigned any storage at all (i.e., the code generated using a const variable will often be almost like you'd use a literal directly, but your source code gets to use a meaningful name instead).

share|improve this answer
In C language const objects has external linkage by default (as opposed to C++, where they have internal linkage). This usually means that C compiler is required to allocate storage for const objects, regardless of how you use it. Only a globally-optimizing compiler can eliminate these const objects. –  AndreyT Nov 29 '09 at 7:57
Of course, in case of local const objects, as in OP's example, it is easier for the compiler to eliminate them. –  AndreyT Nov 29 '09 at 7:58
It's not quite true to say it can be applied to any variable - it's not allowed to use the & operator on variables declared with the register qualifier, or on a bitfield. –  caf Nov 29 '09 at 12:11
add comment

&operator can be applied to anything that has a memory address.You cannot apply & on register variables as they are stored on CPU registers.
Also in C, constants are not compile time constants(i.e always allocated storage), so you can safely take address of a constant variable.But in C++, if you take address of a const variable it will not be a compile time constant and will be allocated storage.
By constants i mean, variables declared with const keywords, literals like A,7, are essentially compile time constants.compiler can directly store them in its symbol table.

share|improve this answer
It's worth noting, however, that in C++ (unlike C), taking the address of a variable with register storage class is allowed (even if doing so doesn't seem to make much sense). –  Jerry Coffin Nov 29 '09 at 5:39
& operator can be applied to anything that has a memory address. Surely literals like 'A' '7' are stored somewhere in memory (runtime), right? Surely I'm missing something :) –  Ankur Nov 29 '09 at 5:45
Simple literals like those are encoded in the instruction, as immediate operands. –  Alex Budovski Nov 29 '09 at 6:05
Not always. CPU instructions don't always allow literals encoded in instructions. Such is dependent on the operation of that SPECIFIC computer type and is outside the C/C++ standards. String literals, for example, are NEVER encoded in such instructions. The reason you can't take the address of a literal is because the address taken from the literal (even in memory if that's the case for that architecture) has no semantic value. This is because the temporary RAM allocated for the literal (if any) is DESTROYED after it is encountered, and any such address taken points to undefined memory. –  Billy ONeal Nov 29 '09 at 7:45
@Ankur: The term you are looking for is lvalue. A C program has a so-called storage - the place where objects are stored. Everything that has place in storage is an lvalue. Constants like 2 might be stored somewhere (not necessariliy though), but even if they are stored somewhere, they are not stored in storage, which means that they are not lvalues and that they have no address as far as C language is concerned. –  AndreyT Nov 29 '09 at 7:55
add comment

Your Answer


