f



pointers to pointers

Hi,
i understood what pointers are and all stuff, and now i'm at the part 
with pointers to pointers and my book doesn't really help on this issue.
let be:
	int a,*p,**q;
how to make use of **q,*q and q ? Which who's address and/or who's value 
contains. I would like some examples like
	p=&a;// p contains the address of a and *p contains it's value
but regarding **q,*q respectively q.
					Thanks.

0
abcdef368 (7)
2/18/2004 3:27:04 PM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

5 Replies
829 Views

Similar Articles

[PageSpeed] 35

apropo wrote:

> Hi,
> i understood what pointers are and all stuff, and now i'm at the part 
> with pointers to pointers and my book doesn't really help on this issue.
> let be:
>     int a,*p,**q;
> how to make use of **q,*q and q ? Which who's address and/or who's value 
> contains. I would like some examples like
>     p=&a;// p contains the address of a and *p contains it's value
> but regarding **q,*q respectively q.
>                     Thanks.
> 

Simply put:

     int a, b;
     int *p, *q;
     int **qq;

     a = 10;
     b = 10;
     p = &b;    // p points to b
     q = &a;    // q points to a
     qq = &q;   // qq points to q

     *q = 20;   // a = 20
     **qq = 30; // a = 30

     q = p;     // q points to b (since p points to b)
     **qq = 40; // b = 40 (since qq points to q)

     *qq = &a;  // q points to a (since qq points to q)

/david

-- 
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
                                 -- unknown
0
noname1 (64)
2/18/2004 3:35:07 PM
ok let's take it easy ...
this line seems complicated, but also easy :
	qq=&q;
and we have a pointer (qq) which points to another pointer (q)
just tell me if it's right:
a)	*qq contains the address of q (p is an address actually), so you can 
say *qq == q
b)	**qq contains the value pointed to by p
and of this one i'm unsure:
c)	qq contains it's address
Did i get it right till now ?

David Rubin wrote:

> apropo wrote:
> 
>> Hi,
>> i understood what pointers are and all stuff, and now i'm at the part 
>> with pointers to pointers and my book doesn't really help on this issue.
>> let be:
>>     int a,*p,**q;
>> how to make use of **q,*q and q ? Which who's address and/or who's 
>> value contains. I would like some examples like
>>     p=&a;// p contains the address of a and *p contains it's value
>> but regarding **q,*q respectively q.
>>                     Thanks.
>>
> 
> Simply put:
> 
>     int a, b;
>     int *p, *q;
>     int **qq;
> 
>     a = 10;
>     b = 10;
>     p = &b;    // p points to b
>     q = &a;    // q points to a
>     qq = &q;   // qq points to q
> 
>     *q = 20;   // a = 20
>     **qq = 30; // a = 30
> 
>     q = p;     // q points to b (since p points to b)
>     **qq = 40; // b = 40 (since qq points to q)
> 
>     *qq = &a;  // q points to a (since qq points to q)
> 
> /david
> 

0
abcdef368 (7)
2/18/2004 4:19:12 PM
On Wed, 18 Feb 2004, apropo wrote:

> Hi,
> i understood what pointers are and all stuff, and now i'm at the part
> with pointers to pointers and my book doesn't really help on this issue.
> let be:
> 	int a,*p,**q;
> how to make use of **q,*q and q ? Which who's address and/or who's value
> contains. I would like some examples like
> 	p=&a;// p contains the address of a and *p contains it's value
> but regarding **q,*q respectively q.
> 					Thanks.

I question whether your book really taught you about pointers if you
cannot make the leap to pointers to pointers.

If I have:

	int i, *p, **q;

The following is true:

	i is an int
	p is an (int *)
	p holds the address of an (int)
	q is an (int **)
	q holds the address of an (int *)

If q holds the address of an (int *) and p is an (int *) then &p is the
address of an (int *). Therefore the following is valid:

	q = &p;

Once that is done:

	*q is the same as p
	**q is the same as *p

If that does not seem clear to you then I have a pictorial method of
displaying pointers that might help. Let me know if you want to see it. It
does not work as well in text and it does when hand drawn.

-- 
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vice.president@whitehouse.gov
0
darrell13 (403)
2/18/2004 4:31:53 PM
apropo wrote:

> ok let's take it easy ...
> this line seems complicated, but also easy :
>     qq=&q;
> and we have a pointer (qq) which points to another pointer (q)
> just tell me if it's right:
> a)    *qq contains the address of q (p is an address actually), so you 
> can say *qq == q

*qq contains the *value* of q, not it's address. That is, *qq != &q.

Think of it in terms of this picture of memory address (need fixed font)

    00      01      02      03      04
   +--+    +--+    +--+    +--+    +--+
   |01|--->|02|--->|10|    |04|--->|20|
   +--+    +--+    +--+    +--+    +--+
    qq      q       a        p       b

Values on top are memory addresses. Values in boxes are values stored at that 
memory location. Pointer dereferences follow the arrows. Assignment puts a value 
in a box.

> b)    **qq contains the value pointed to by p
> and of this one i'm unsure:
> c)    qq contains it's address

The adress of p.

> Did i get it right till now ?

Yes.

/david

-- 
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
                                 -- unknown
0
noname1 (64)
2/18/2004 4:47:19 PM
Groovy hepcat apropo was jivin' on Wed, 18 Feb 2004 15:27:04 GMT in
comp.lang.c.
pointers to pointers's a cool scene! Dig it!

>i understood what pointers are and all stuff, and now i'm at the part 
>with pointers to pointers and my book doesn't really help on this issue.

  There is nothing special about pointers to pointers. You know that a
pointer is an object that holds the address of another object. Well, a
pointer to pointer is an object that holds the address of another
pointer. That's really all there is to it.
  So, given the following

int i = 42;
int *pi = &i;
int **ppi = π

then

1) i contains the value 42,
2) pi contains the address of i,
3) *pi is i, so
4) *pi contains the value 42,
5) ppi contains the address of pi,
6) *ppi is pi, so
7) *ppi contains the address of i,
8) **ppi is i, so
9) **ppi contains the value 42.

So, where you need an int value, you can use either

1) 42
2) i,
3) *pi, or
4) **ppi.

And where you need a pointer to int, you can use

1) &i,
2) pi, or
3) *ppi.

And where you need a pointer to pointer to int, you can use

1) &pi, or
2) ppi.

To change i's value to 23, you can use either

1) i = 23,
2) *pi = 23, or
3) **ppi = 23.

And given the following

int i2;

to make pi point at i2, use either

1) pi = &i2, or
2) *ppi = &i2.

  How simple can it get?

-- 

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
0
shaggy (99)
2/22/2004 7:42:25 AM
Reply: