By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Pointers to pointers

P: n/a
What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?

cman

Feb 18 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
In article <11**********************@s48g2000cws.googlegroups .com>,
cman <ti****@gmail.comwrote:
>What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?

cman
If you want to modify an object passed in the argument list of a
function from within the function, you must pass a pointer to the object
as the argument. So, if you want to modify a pointer, you should pass a
pointer to the pointer as the function argument.

Feb 18 '07 #2

P: n/a
cman wrote:
What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?
This is just a special case of "What do pointers to (Things)
accomplish?" The answer is that they provide a way to access
(Things) without needing to know their names -- indeed, without
them even needing to have names at all. Sometimes the (Things)
are ints, sometimes they are doubles, sometimes they are structs.
And sometimes, sometimes, you may want "anonymous" access to a
(Thing) that is itself a pointer.

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 18 '07 #3

P: n/a
cman wrote:
What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?
Smells like homework. What are your thoughts on the matter? What have
you found about pointers to pointers in your text?


Brian
Feb 18 '07 #4

P: n/a
cman wrote:
What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?

cman
This sounds like a homework question :-). But I'll help you by giving hints.

Can you write a function which takes two dimensional char arrays and prints
it contents ? How will you pass two dimensional array to this function ?

Can you study how passing command line input to a program works in C ?
something like

int main(int argc, char **argv)

On a advanced topic, can you write a function which inserts a node at the
head of link list ? How will you pass head pointer to this function ?

Cheers,
Tejas Kokje
Feb 18 '07 #5

P: n/a
cman skrev:
What do pointers to pointers accomplish?
Nothing really.
How does having a pointer to
a pointer help a design or aglorithm?
Well, it doesn't.
Where are they normally used?
Since C lacks call by reference, when you want a function to modify a
pointer you have to pass it a pointer to the pointer.
August
Feb 18 '07 #6

P: n/a
Tejas Kokje <bi*************@gmail.comwrites:
cman wrote:
>What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?

cman

This sounds like a homework question :-). But I'll help you by giving hints.

Can you write a function which takes two dimensional char arrays and prints
it contents ?
[...]

Not really. A two dimensional array is an array of arrays. C doesn't
provide a good way of passing such a beast to a function, even using
pointers, unless the second dimension is fixed. You can pass a
pointer to the first element and do the indexing calculations yourself
(though it could be argued that that invokes undefined behavior).

What you can do is create something that *acts like* a two-dimensional
array. One obvious way to do this is to create an array of pointers,
where each pointer points to the first element of a row of the
"array". This is actually more flexible than a two-dimensional array,
in that it can be "ragged" (not all the rows have to be of the same
length) -- but it's more difficult because you have to manage the
memory allocation for each row *and* for the array of pointers. The
argv parameter to main() is an example of this.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 18 '07 #7

P: n/a
Tejas Kokje wrote:
cman wrote:
>What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?

This sounds like a homework question :-). But I'll help you by giving
hints.
.... snip ...
>
On a advanced topic, can you write a function which inserts a node at
the head of link list ? How will you pass head pointer to this
function ?
I will avoid the confusion of double stars with:

link *inserthead(link* root, whatever data);

and the usage is:

root = inserthead(root, data);

and the code for inserthead probably looks like:

link *inserthead(link *root, whatever data);

link *tmp;

if (tmp = malloc(sizeof *tmp) {
tmp->next = root;
/* the following may require further mallocation */
/* depending on the definition of link. Also */
/* other fields in *tmp may need initialization */
tmp->data = data;
root = tmp;
else {
/* optional - announce lack of memory */
}
return root;
}

If you use NULL to signal error, then you want to treat inserthead
in the same manner as realloc, by receiving its return value in a
temporary, and copying that to root only if it is non-NULL.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 19 '07 #8

P: n/a
On Feb 18, 3:56 pm, CBFalconer <cbfalco...@yahoo.comwrote:
Tejas Kokje wrote:
cman wrote:
What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?
This sounds like a homework question :-). But I'll help you by giving
hints.

... snip ...
On a advanced topic, can you write a function which inserts a node at
the head of link list ? How will you pass head pointer to this
function ?

I will avoid the confusion of double stars with:

link *inserthead(link* root, whatever data);

and the usage is:

root = inserthead(root, data);

and the code for inserthead probably looks like:

link *inserthead(link *root, whatever data);

link *tmp;

if (tmp = malloc(sizeof *tmp) {
tmp->next = root;
/* the following may require further mallocation */
/* depending on the definition of link. Also */
/* other fields in *tmp may need initialization */
tmp->data = data;
root = tmp;
else {
/* optional - announce lack of memory */
}
return root;
}

If you use NULL to signal error, then you want to treat inserthead
in the same manner as realloc, by receiving its return value in a
temporary, and copying that to root only if it is non-NULL.
Point noted. But I was not asking OP to find out ways to avoid using
double pointers. In fact, OP was interested in learning about where
pointers to pointers can be useful and I as just suggesting him a
case.

Tejas Kokje

Feb 19 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.