[Followups moved to comp.lang.c.]
In article <AM5Gk.848$P5.6 26@nwrddc02.gni link.net>,
James Kuyper <jameskuyper@ve rizon.netwrote:
Obviously head has to be non-null, but the question is why the
function has to check for it, rather than relying on the user to pass
in a good value. Presumably in this case it's specified that the
function can be called with a null pointer.
The reason it uses a pointer-to-pointer is so that you can pass in
a "list" variable, which will be modified. The natural idiom will
be
node *list;
... build a list ...
RemoveHead(&lis t);
If the argument were a plain pointer-to-node, you would have to
instead write
list = RemoveHead(list );
Given that the normal use will be to pass the address of a variable, I
don't see much point in allowing a null argument. Perhaps the idea is
to be able to (say) call RemoveHead() on a two-node list three times
without causing an error, just as (cdr (cdr (cdr '(1 2)))) is nil on
traditional Lisps, but I think that too often hides mistakes.
-- Richard
--
Please remember to mention me / in tapes you leave behind.
In article <AM5Gk.848$P5.6 26@nwrddc02.gni link.net>,
James Kuyper <jameskuyper@ve rizon.netwrote:
> if (head && *head) { /* Corrected code */
>1. Why is the purpose of 'head' in the 'if' condition? Shouldn't
>'*head' suffice?
>'*head' suffice?
>No. If head is a null pointer, then every other statement in the program
(including the check of whether *head is null) has undefined behavior.
(including the check of whether *head is null) has undefined behavior.
function has to check for it, rather than relying on the user to pass
in a good value. Presumably in this case it's specified that the
function can be called with a null pointer.
The reason it uses a pointer-to-pointer is so that you can pass in
a "list" variable, which will be modified. The natural idiom will
be
node *list;
... build a list ...
RemoveHead(&lis t);
If the argument were a plain pointer-to-node, you would have to
instead write
list = RemoveHead(list );
Given that the normal use will be to pass the address of a variable, I
don't see much point in allowing a null argument. Perhaps the idea is
to be able to (say) call RemoveHead() on a two-node list three times
without causing an error, just as (cdr (cdr (cdr '(1 2)))) is nil on
traditional Lisps, but I think that too often hides mistakes.
-- Richard
--
Please remember to mention me / in tapes you leave behind.
Comment