nsimplex
nsimplex

Reputation: 489

Understanding the semantics of C pointer casts when applied to literals

Is there any documentation specifying the meaning of a pointer cast applied to a literal in C?

For example:

int *my_pointer = (int *) 9 

Is this compiler-dependent or part of the standard?

Edit: deleted misleading note based on comment below, thanks.

Upvotes: 0

Views: 125

Answers (2)

Alexey Frunze
Alexey Frunze

Reputation: 62086

Pointer to integer and integer to pointer conversions are implementation-defined (see Annex J of the C standard).

Upvotes: 3

ouah
ouah

Reputation: 145899

int *my_pointer = (int *) 9 

This does not point to the literal 9. It converts the literal 9 to a pointer to int. C says the conversion from an integer to a pointer type is implementation-defined.

int *my_pointer = &(int) {9};

This does. It makes my_pointer points to an int object of value 9.

Upvotes: 5

Related Questions