Setting an address and initializing a pointer

Firstly, I regret my poor English, this is not my first language.

This is my first time I have studied pointers and I have found something really strange.

In the book I am reading, marker *means a variable labeled "pa".

But when I try to initialize a pointer

int a;
int *pA =&a;

(in this case they used *pA) and then changed it,

* pA = & a;

not working as well

pA = & a;

work.

So my query is: "Is there a difference between initializing pointers and just replacing?"

+3
source share
4 answers
int a;

This allocates an integer on the stack

int* pA = &a;

, . "*" .

*pA = &a;

'*' , : ", pA ", int. int a, .

pA = &a;

, . pA .

+3

,

int *pA;

*pA , , .

&a, Address-of Operator, , a ( ).

, pA = &a, a pA, .

*pA ( ), *pA - pA = &a.

0

, , . , *. , *pA , ( ). , .

, :

int a; 
int *pA = &a;

:

pA = &a;

:

*pA = &a;

, " , pA= a."

:

*pA = a;

, pA. , .

0

C, " .

int

int *pa;

, pa int * *pa int.

int pa, ints *pa.
"".

*pa = 42;
pa = &a;

In the declaration itself, you can "convert" it to a definition by specifying an initialization value. The definition is for a patype object int*, not for *pa.

int *pa = &a; /* initialize pa, which takes a `int*` */
pa = &b;      /* change `pa` with a different `int*` */
*pa = 42;     /* change the value pointed to by `pa` */
0
source

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


All Articles