I
Ike Naar
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
#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