Pointers to Pointers 101
Lets say you have an int
variable x
: int x;
.
- A portion of memory will be assigned to
x
large enough to hold an int
.
- The memory assigned to
x
has an address in the process memory map.
- To see the address where
x
is located in memory, use:
printf("x's memory address = %p
", (void*)&x);
&x
means address of x
.
- To see the integer value stored in
x
, use:
printf("x=%d
", x);
x
can only be manipulated directly within it's scope of existence. For example, it can be manipulated within the function in which it is declared:
x = 42;
- Outside its scope of definition, the value of
x
can be manipulated if its memory address is known.
- If the value of
x
(ie: 42) is passed to a function(x
), that function cannot manipulate the value of x
.
- If the address of
x
is passed to a function(&x
), that function can manipulate the value of x
.
Now, lets say that you have a pointer variable p
that assumes it points to an int
: int *p;
- A portion of memory will be assigned to
p
large enough to hold a memory address.
- The memory assigned to
p
has an address in the process memory map.
&p
means address of
p.
- To see the address where
p
is located in memory, use:
printf("p's memory addr = %p
", &p);
- To see the address where
p
is pointing, use:
printf("Address where p
is pointing: %p
", p);
- To see the alleged integer being pointed to, use:
printf("int = %d
", *p);
- The value of
p
is an address in memory; and p
can be set to point to any address, whether or not that address actually exists in the process memory map.
- The address of an object is a pointer to that object. Hence, To cause
p
to point to x
:
p = &x
- Whatever
p
is pointing at can be referred to by the pointer type (type int for p
).
- The amount of memory assigned to
p
will vary, depending on the architecture of the machine; and how it represents an address.
p
can only be manipulated directly within it's scope of existence. For example, it can be manipulated within the function in which it is declared:
p = &x;
or p = NULL
- Outside its scope of existence, the value of
p
can be manipulated if its' memory address is known.
- If the value of
p
(an address) is passed to a function(p
), that function cannot manipulate 'p' to change where it points.
- If the address of
p
is passed to a function(&p
), that function can manipulate what p
points to.
Now, lets say that you have a pointer to a pointer variable pp
that assumes it points to a pointer to an 'int': int **pp;
...or: void inserts(customer **tmp) :
- The address of a pointer is a pointer to a pointer.
- ...
Back to the question
Q: Any way to do the same work without using pointer to pointer?
No. Assume the following:
void inserts(customer **tmp);
...
{
customer cust;
custPtr custPtr = &cust;
inserts(&custPtr);
...
The inserts()
function requires the address of a pointer in order to manipulate where custPtr
points.
If instead:
void inserts2(customer *tmp);
...
{
customer cust;
custPtr custPtr = &cust;
inserts2(custPtr);
...
The insert2()
would get a copy of the value of custPtr
, which is the address of cust
. Hence, insert2()
could modify the value(s) of cust
, but could not change where custPtr
is pointing.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…