[comp.lang.c] Is this pointer manipulation portable?

rtm@grenada.UUCP (Richard Minner) (10/21/89)

I'd appreciate opinions as to the portability of the code below.  It
plays some games with data pointers to be able to support linked list
nodes of various sizes (rather than having to duplicate the code
for each node type).  Also, can you see a way to accomplish the same
thing in a more portable way?

The idea is for a client to pass a contiguous array of (unused) nodes,
plus the (arbitrary) node size.  The routine treats the array as a
char array, and walks through it linking all the nodes together, then
adding the result to the client's list.

It is specified that the array argument must be suitably alligned for
the client node type of interest, and the node size must be as given
by sizeof (i.e. including padding).

Note: I stripped out arg and error checking so don't bother with such.

typedef struct LLL_node { struct LLL_node *next; }; LLL_NODE;

LLL_add_array(list, array, num, nodesize)
LLL_NODE  **list;	/* Pointer to pointer to first node of list.	*/
void	  *array;	/* Array of client nodes.			*/
int	  num;		/* Number of nodes in the array.		*/
int	  nodesize;	/* Size of one node (e.g. sizeof(client node)). */
{
    LLL_NODE	    *orig_head;
    unsigned char   *this, *next;

    /* ----- save original head, set head to the first array node */
    orig_head = *list;
    *list = (LLL_NODE*)array;

    /* ----- link the array nodes together */
    this = (unsigned char*) array;

    while (--num > 0)
    {
	next = this + nodesize;
	((LLL_NODE*)this)->next = (LLL_NODE*)next;
	this = next;
    }

    /* ----- point last node at original list head */
    ((LLL_NODE*)this)->next = orig_head;
}

Might this even be ANSI (pANS) compatible?  If not, can you spot any
serious porting pittfalls?
-- 
Richard Minner  || {uunet,sun,well}!island!rtm     (916) 447-7081 ||
                || Island Graphics Corporation     Sacramento, CA ||