Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using & (addressof) with const variables in C

Tags:

c

constants

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;

}

like image 334
TL36 Avatar asked Mar 01 '23 00:03

TL36


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

like image 152
AnT Avatar answered Mar 08 '23 05:03

AnT