In article <1171819744.426 037.176610@s48g 2000cws.googleg roups.com>,
cman <tilakb@gmail.c omwrote:
>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.
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.
>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_Keit h) kst-u@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.
>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(lin k* root, whatever data);
and the usage is:
root = inserthead(root , data);
and the code for inserthead probably looks like:
link *inserthead(lin k *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.
"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
On Feb 18, 3:56 pm, CBFalconer <cbfalco...@yah oo.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(lin k* root, whatever data);
>
and the usage is:
>
root = inserthead(root , data);
>
and the code for inserthead probably looks like:
>
link *inserthead(lin k *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.
Comment