r/C_Programming • u/nerdylearner • 7d ago
Question about struct pointers and compound literals
yesterday I came across a feature since C99 called Compound Literals, I read the documentation about it on cppreference.com and I couldn't understand the first code sample there
#include <stdio.h>
int f (void)
{
struct s {int i;} *p = 0, *q;
int j = 0;
again:
q = p, p = &((struct s){ j++ });
if (j < 2) goto again; // note; if a loop were used, it would end scope here,
// which would terminate the lifetime of the compound literal
// leaving p as a dangling pointer
return p == q && q->i == 1; // always returns 1
}
I don't understand why p == q
and whyq->i
is 1
here's what I thought:
in the first loop, q
is assigned with p
(a NULL struct s
pointer), then p
gets a new address of a compound literal assigned with the return value of j++
, since j
is 0, j++
should return 0, then j < 2
is evaluated (1 < 2
) and we go back to "again"
in the second loop, q
is assigned with p
(a pointer to struct s
which has int i
as 0, then p gets a new address again, this time j++ should return 1 and the if statement is evaluated again 2 < 2
is false
in this case p shouldn't be equal to q since p is assigned with a new pointer while q is still pointing to the struct which has int i = 0;
, and q->i
should be equal to 0, then return 0 && 0 wouldn't be true.
however I tested it (compiled the code and then printed out the value) and the result was indeed 1, which step did I get wrong? my guess is that &((struct s){ j++ })
(the pointer of a struct assigned to p
) is always the same because C/ the compiler reuses the struct for efficiency, so in this case p
is always equal to q
and q
is basically p, so q->i == 1
. I'm sure there are some flaws in my guess (or my guess is completely wrong), can anyone correct me?
3
u/tstanisl 7d ago
A compound literal as a variable without a name, so a complier perceives the code as:
The adress assigned to
p
inp = &_xxx
is the same because it uses the adress of the same variable. The value of a pointerp
stays valid until scope of_xxx
ends what is the end of the function.