473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.

Nov 14 '05 #1
5 1965
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
Nov 14 '05 #2
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


Nov 14 '05 #3
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
Nov 14 '05 #4
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 vi************@whitehouse.gov
Nov 14 '05 #5
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"?
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.