tags:

views:

227

answers:

3

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;

}

A: 

&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.
Edit
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.

Neeraj
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
Ankur
Simple literals like those are encoded in the instruction, as immediate operands.
Alex
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
@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
+3  A: 

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).

Jerry Coffin
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
Of course, in case of local `const` objects, as in OP's example, it is easier for the compiler to eliminate them.
AndreyT
caf
+3  A: 

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.

AndreyT
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