This question is meant to be a FAQ entry for all initialization/assignment between integer and pointer issues.
I want to do write code where a pointer is set to a specific memory address, for example 0x12345678
. But when compiling this code with the gcc compiler, I get "initialization makes pointer from integer without a cast" warnings/errors:
int* p = 0x12345678;
Similarly, this code gives "initialization makes integer from pointer without a cast":
int* p = ...;
int i = p;
If I do the same outside the line of variable declaration, the message is the same but says "assignment" instead of "initialization":
p = 0x12345678; // "assignment makes pointer from integer without a cast"
i = p; // "assignment makes integer from pointer without a cast"
Tests with other popular compilers also give error/warning messages:
- clang says "incompatible integer to pointer conversion"
- icc says "a value of type
int
cannot be used to initialize an entity of typeint*
" - MSVC (cl) says "initializing
int*
differs in levels of indirection fromint
".
Question: Are the above examples valid C?
And a follow-up question:
This does not give any warnings/errors:
int* p = 0;
Why not?