Simple explanation of pointer C

I just started working with pointers to C, and I hung up on something simple. I want to make sure I understand correctly.

Let's say I have something like this:

int *buff;

From what I read

*buff refers to the value that buff is currently pointing to
&buff refers to the address

But I'm stuck:

What does buff mean? This refers to the location & buff in memory, which indicates the location of the * buff value?

Thanks: 3

-jtsan

+3
source share
5 answers

int *means "pointer to int". So here buffis a pointer to int. To make things easier, let's also say:

int x = 5; 
int *buff = &x;

x, , 5. &x " x". buff x. , x 0x1000. , buff : 0x1000.

*buff ", ", 5.

&buff " ": , buff .

, , , , .

. . . "100" . " " . ( 100 , 0, .) .

+---+---+---+---+---+--
|   |   |   |   |   | ...
+---+---+---+---+---+--
100  101 102 103 104  ...

, , , int - . . int a . - . , int *b = &a. int *b , - , , &a, "".

int  a = 5;
int *b = &a;
  a       b 
+---+---+---+---+---+--
| 5 |   |100|   |   | ...
+---+---+---+---+---+--
 100 101 102 103 104  ...

, . ( , , , , , ..), 99% .

int ...

int a = 5;
char b = 2;
  a   a   a   a   b
+---+---+---+---+---+--
| 0 | 0 | 0 | 5 | 2 | ...
+---+---+---+---+---+--
 100 101 102 103 104  ...
+10

:

int value = 13;
int *buff = &value;

:

*buff == 13

buff value 13.

, , &buff; buff, &buff int **, . , &value value; int * ( - buff).

Plain buff int * . value.

+4

* buff ,

& buff

. , , , . &buff , , &buff , buff.

"buff"?

buff , &buff, .. , buff. - , .

, :

  • int *buff; - int,
  • int foo; buff = &foo; - foo
  • *buff = 5; - , buff (foo) 5
  • int **buff_ptr = &buff; - int
+3

buff " int". * buff " ", .. int. & buff " ", , int ( ).

+3

, *buff buff, &buff buff. (&) , buff (*), *&buff, buff.

. , , .

+1

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


All Articles