Convert int to int * and then back to int

I missed something obvious here, but consider the following,

int k = 10;
int *p = &k;
int i = (int)p;

higher,

warning: cast from pointer to integer of different size

and further

int k = 10;
int *p = &k;
int i = p;

causes,

warning: initialization makes integer from pointer without a cast

a little googling will lead me

Convert pointer to integer

which advises to use uintptr_t,

int k = 10;
int *p = &k;
int i = (uintptr_t)p;

above no errors, no warnings. So my question is: k is an int, and p is an int pointer pointing to k, why does the dereference p occur?

+5
source share
7 answers
int k = 10;
int *p = &k;
int i = *p;

Note the * before p on the last line. Expand the pointer to get the int it points to.

EDIT: uintptr_t, int = (uintptr_t) p; , i, - k, 10. , ( , , , ), .

+9

, p, ( ) int*. *, :

int i = *p;
+3

, int.

int i = *p;

, , , .

+2

int , , . , , .

, , ( ?). .

, k i, K

int i = *p;
+1

i k ( p). . , , , casting unsigned.

, 32- 64. 8- , 4- .

+1

int k = 10;
int *p = &k;
int i = *p;

k = 10

p =

& k =

* p = 10

0

uintptr_t , int, . ( , , ), , . , , uintprt_t int. , , .

int - . -, - , .

0

All Articles