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