James wrote:
> ->> listy = (line *) malloc (sizeof (line));
> ->
> ->No need to cast the return of malloc.
>
> well it gets rid of an annoying warning.
There shouldn't be any warning without the cast. malloc() is defined
to return `void *', which can be implictly cast to any data pointer
without a warning.
The most common cause of a warning is failing to declare malloc(),
i.e. by `#include'ing <stdlib.h>. I notice that you didn't #include
<stdlib.h>; on an architecture where sizeof(int) != sizeof(void *),
this could cause actual problems.
> ->/* It's easier to add at the head */
>
> i was taught to add at the tail!
Right. Could you please shoot the person who taught you this before
they do any more damage :)
Adding to the head of a linked list isn't just a simplicity issue. If
the list is shared (i.e. you have multiple pointers to it), you can
add to the head of the list without affecting anything else which
references the list.
Adding to the tail involves modifying the list, which will affect
everything that references it.
Aside:
Lisp (which uses linked lists extensively) has both destructive and
non-destructive versions of several common list manipulation
functions, e.g. conc/nconc (concatenate two lists), reverse/nreverse
(reverse a list) etc.
The n* versions modify the argument list(s), whereas the normal
versions duplicate parts of the list(s) as necessary. Using the n*
versions is much trickier than using the normal versions (I've just
fixed a bug in an XEmacs package caused by using nreverse on a list
which was referenced elsewhere).
--
Glynn Clements <[EMAIL PROTECTED]>