How C ++ dereferencing works

I'm having trouble understanding what happens when I call the & * pointer

int j=8;
int* p = &j;

When I print in my compiler, I get the following

j = 8 , &j =  00EBFEAC  p = 00EBFEAC , *p = 8 , &p =  00EBFEA0 
&*p= 00EBFEAC

cout << &*p gives  &*p = 00EBFEAC which is p itself

& and * have the same operator priority. I thought that * p would translate to the &(*p)--> &(8)expected compiler error as well.

How does the compiler output this result?

+1
source share
3 answers

You are faced with something interesting: variables, strictly speaking, are not values, but refer to values. 8is an integer value. After int i=8, irefers to an integer value. The difference is that it can refer to a different value.

, i , .. , , i. C , , : i=8; printf("%d", i) , printf("%d", 8). , , - . C . . . . , int. f return, , ( ):

int i = 1;
int g(){ return 1; }  // literal
int f(){ return i; }  // variable

, sigle. ( icc) g:

    movl      $1, %eax                                      #5.17

: 1 eax.

, f

    movl      i(%rip), %eax                                 #4.17

rip plus offset eax. , () .

. return *i, 1, return i , — .

,

int j=8;
int* p = &j;
printf("%d\n", *p);

8 (.. p ); &(*p) , p ( , p), &(8). , (, , L-, p), , .

, — Algol68 —, int i=8 8. , ref int m = loc int := 3. , , , ref ref int, .

+4

j - int 8 00EBFEAC.

& j j (00EBFEAC).

int * p = & j p, int *, , int. & j, int → , .

* p , , p. , p, int, * p int, 8.

& p - , p

& * p , p- , p . & (* p) → & (j) → 00EBFEAC

+2

&j ( &(j)). , j 8 &8, ? l, , .

L "lvalue" , j = 10 *p = 12. r, j + 10 8, , , .

. ++ , ( ).

+1

Source: https://habr.com/ru/post/1608784/


All Articles