& operator cannot be applied to constants in C

According to the C programming language With Kernigan and Richie, page 94

& operator cannot be applied to constants

const int u = 9; printf ("\nHello World! %u ", &u); 

So why does this work?

+5
source share
3 answers

You do not understand between constant and const . Both languages ​​are two different things in C.

& operator cannot be applied to constants

In C programming, this means that you cannot use &(address operator) in a literal constant to get the address of a literal constant.

Examples:

 &10 

or

 &('a') 

If you use the & operator over a literal constant, the compiler will throw an error because the constant object does not have a corresponding address.

+6
source

According to Draft Standard §6.5.3.2 ¶1, the use of the address operator must be subject to the following restriction:

The operand of the unary operator & must be either the function name, the result of the [] operator or the unary * or lvalue, which indicates an object that is not a bit field and is not declared by the register class specifier.

The value of lvalue is given in §6.3.2.1 ¶1 :

The lvalue value is an expression (with an object type other than void ) that potentially denotes an object.

According to §3.15 ¶1 the object is:

a data storage area in the runtime whose contents can represent values.

Now the constant is not an lvalue value; a constant has a value, but a constant does not indicate an object, and a value cannot be assigned to a constant. Thus, a constant is not a function designation, is not the result of the [] operator or unary * and is not an lvalue value, which means that accessing the constant address is a violation of the restriction. A diagnostic message should be issued using the appropriate implementation.

On the other hand, given const int u = 9; , u is a const qualified variable of type int . This declaration reserves memory for the variable, and u is the value of lvalue. Using const does not mean that u is a constant, but the object indicated by the identifier u is const (this is not the same thing). It might be better to think of const qualified variables as read-only; it is a promise made by the program that the object indicated by u will not be modified. Since u is an lvalue here (it is not a bit field and is not declared with the register keyword), it must accept its address with &u .

Note that the published code has undefined behavior, since the correct printf() conversion specifier for print addresses is %p ; its argument must be distinguished to (void *) . Inappropriate specifications and conversion arguments result in undefined behavior. So the correct code is:

 const int u = 9; printf ("\nHello World! %p ", (void *) &u); 
+1
source

No, to get a variable address in memory, we can apply a pointer to any of them, since the CPU allocates memory for everyone. On the other hand, this question has already been asked.

The term "constant" in C really means only literal constants, for example, 2. The const-const object is not a "constant" in the terminology of C

Question link

0
source

Source: https://habr.com/ru/post/1271382/


All Articles