tricky assignment statemenent

From: Ike Naar (nospam_at_nospam.invalid)
Date: 12/31/04


Date: 31 Dec 2004 06:18:27 +0100

Consider the following code:

#include <stdlib.h>

struct s
{
  /* ... */
  struct s * next;
};

void f(struct s * q)
{
  struct s * p = malloc(sizeof *p);
  if (p != 0)
  {
    p = p->next = q; /* is this safe? */
  }
}

In the context given above, is the statement "p = p->next = q;"
safe and equivalent to "p->next = q; p = q;"
or does it invoke undefined behaviour because p is modified and
dereferenced at the same time?

Kind regards,
Ike

-- 
mail to ike at iae dot nl


Relevant Pages