O
Old Wolf
The code is:
extern int x[1];
char *ptr1 = 8 + (char *)&x;
char *ptr2 = (char *)(8 + (unsigned)&x);
My understanding is that the ptr1 declaration is correct but the
ptr2 is not, and the compiler can reject it (even if the compiler
has a meaningful definition of integer<->pointer conversions).
The expression "&x" certainly is an address constant. C99 6.6/7
says that the initializer of a static object can be an address
constant plus or minus an integer constant, which is certainly
what we have for ptr1. It seems to be intentionally worded to
allow the initializer to not point to an lvalue in this case.
Howver, it 6.6/9 says that an address constant must be either a
null pointer, a function designator, or a pointer to an lvalue
designating an object. (It goes on to give rules for how that pointer
can be formed). Since (8 + (unsigned)&x) could be pointing off
into the wild, it does not designate an lvalue, so it is not an
address
constant, so the initialization is ill-formed.
Is this correct?
extern int x[1];
char *ptr1 = 8 + (char *)&x;
char *ptr2 = (char *)(8 + (unsigned)&x);
My understanding is that the ptr1 declaration is correct but the
ptr2 is not, and the compiler can reject it (even if the compiler
has a meaningful definition of integer<->pointer conversions).
The expression "&x" certainly is an address constant. C99 6.6/7
says that the initializer of a static object can be an address
constant plus or minus an integer constant, which is certainly
what we have for ptr1. It seems to be intentionally worded to
allow the initializer to not point to an lvalue in this case.
Howver, it 6.6/9 says that an address constant must be either a
null pointer, a function designator, or a pointer to an lvalue
designating an object. (It goes on to give rules for how that pointer
can be formed). Since (8 + (unsigned)&x) could be pointing off
into the wild, it does not designate an lvalue, so it is not an
address
constant, so the initialization is ill-formed.
Is this correct?