I just started with working with pointers in C and I'm hung up on something simple. I want to make sure I understand it correctly.
Say I have something like this:
int *buff;
From what I read,
*buff refers to the value tha开发者_如何学Ct buff is currently pointing to
&buff refers to the address
But what I'm stuck on is:
What does just "buff" refer to? Does it refer to the location of &buff in memory which points to the location of the value *buff?
Thanks a bunch! :3
-jtsan
int *
means "a pointer to an int." So, here, buff
is a pointer to an int. To make things simpler, let's also say:
int x = 5;
int *buff = &x;
x
, an integer, is set to 5.
&x
means "the address of x". So buff
contains the address of x
. For the sake of argument, let's say that x
is stored at memory address 0x1000. So buff
itself is also a number: 0x1000.
*buff
means "the thing pointed to by buff," in this case 5.
&buff
means "the address of buff": the address at which number buff
itself is stored in memory.
I'd like to share a general technique that I used to learn how pointers work when I was starting out.
Get a big sheet of graph paper and lay it lengthwise on the table in front of you. This is your computer's memory. Each box represents one byte. Pick a row, and place the number '100' below the box at far left. This is "the lowest address" of memory. (I chose 100 as an arbitrary number that isn't 0, you can choose another.) Number the boxes in ascending order from left to right.
+---+---+---+---+---+-- | | | | | | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
Now, just for the moment, pretend an int is one byte in size. You are an eight-bit computer. Write your int a
into one of the boxes. The number below the box is its address. Now choose another box to contain int *b = &a
. int *b
is also a variable stored somewhere in memory, and it is a pointer that contains &a
, which is pronounced "a's address".
int a = 5;
int *b = &a;
a b +---+---+---+---+---+-- | 5 | |100| | | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
Now you can use this model to visually work through any other combinations of values and pointers that you see. It is a simplification (because as language pedants will say, a pointer isn't necessarily an address, and memory isn't necessarily sequential, and there's stack and heap and registers and so on), but it's a pretty good analogy for 99% of computers and microcontrollers.
You can extend the model for real four-byte int
s too...
int a = 5;
char b = 2;
a a a a b +---+---+---+---+---+-- | 0 | 0 | 0 | 5 | 2 | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
Given the declarations:
int value = 13;
int *buff = &value;
You're right:
*buff == 13
because buff
points to value
and that contains 13.
You're probably misunderstanding what &buff
refers to; it is the address of the variable buff
, and so the type of &buff
is int **
, a pointer to a pointer to an integer. By contrast, the &value
is the address of the variable value
; it is of type int *
(so the initialization did not need any cast - it is of the correct type to be stored in buff
).
Plain buff
is an int *
or pointer to an integer. It contains the address of the variable value
.
buff was declared as "pointer to int". using *buff is like saying "what buff points to", i.e the int value. using &buff is like saying "the address of buff", which is the address of the pointer, that points to int (another level of indirection).
*buff refers to the value that buff is currently pointing to
Yes
&buff refers to the address
No. At least not if you're thinking what I think you're thinking. &buff
does not refer to the address of what it's currently pointing to, &buff
refers to the address in memory in which buff
itself is located.
What does just "buff" refer to?
buff
refers to what you thought &buff
was, i.e. the address of what buff
points to. The value of a pointer is the address of what it points to.
Here are some examples to help you out:
int *buff;
-- declare a pointer to int which currently doesn't point anywhereint foo; buff = &foo;
-- point buff to the address of foo*buff = 5;
-- change the contents of what buff points to (foo) to 5int **buff_ptr = &buff;
-- declare a pointer to a pointer to int and point it at the address of buff
You are correct in that *buff
refers to the location buff
points to, but &buff
refers to the address of buff
itself. The address (&
) of the data that buff
points to (*
) is *&buff
, or more commonly just buff
.
Good luck. Pointers are pretty tricky, but you look like you're on the right track.
精彩评论