Evaluation order of assignment.

Chris Torek chris at mimsy.UUCP
Wed Aug 17 12:37:07 AEST 1988


In article <957 at orion.cf.uci.edu> schmidt at bonnie.ics.uci.edu (Douglas
C. Schmidt) writes:
>Is the following always guaranteed to produce the "intended" result:
>
>struct list foo()
>{
>   struct list head;
>   
>   return(head->next = head = (struct list *) malloc(sizeof(struct list)));
>}

Aside from the fact that there are two `*'s missing, and that malloc
can return NULL, the answer is no.

>My intention is to create a dummy node in a circularly-linked list,
>and assign the dummy node's next field to point to the head of
>the list.  Since assignment associates from right-to-left this
>will alway work, right (cryptic style notwithstanding !! ;-)).

Not so.  If you must use cryptic style, try

	/* assuming emalloc() is declared, and is like malloc but
	   never returns NULL: */
	struct list *foo()
	{
		struct list *head;

		return (head = (struct list *)emalloc(sizeof *head),
			head->next = head);
	}
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list