What is the result of &pointer in C?
What is the result of the following line:
int* ptr;
printf("%x, %x\n", ptr, &ptr);
I know that ptr
is an address in a memory, but what is &开发者_JS百科amp;ptr
?
&ptr would be the address for the memory location that ptr is held in. Essentially it is a pointer to a pointer.
It's the address of the memory location that contains the address of the original memory location (i.e., it's a "pointer to a pointer").
&ptr
returns the address of the pointer variable... pointer to a pointer if you will.
This is often used to allow the function to change where the pointer is actually pointing.
ptr
is not just "an address in memory". ptr
is an lvalue, an object in memory that holds an address. Every object in memory has its own address, regardless of what it holds.
Since ptr
is an object in memory, it also has its own address. That address is exactly what you get when you do &ptr
.
a pointer is just a reference to the location of some data in memory. *pointer gives you the value stored in that memory location. The & operator returns the actual memory address which, in this case, is a pointer.
In C, pointers are just storage containers that hold an address of some other chunck of data. In this case, ptr holds the address of some int, and it is itself just some piece of data in memory. So &ptr is the address of the variable that hold the address of some int.
&ptr could be stored only in int **var
or a double pointer variable, so &ptr is nothing but the address of the ptr containing another address.
精彩评论