I can't imagine this isn't already duplicate, but I can't easily find the answer since the more complex scenarios specifically to C++ seem to dominate the discussion0.
Is it legal to take take the address of a temporary constructed in the parameter list of a function call in C99?
For example, something like init_list
or init_desig_init
as follows:
typedef struct {
int x;
int y;
} point_t;
int manhattan(point_t *p) {
return p->x + p->y;
}
int init_list() {
return manhattan(&(point_t){1, 2});
}
int init_desig_init() {
return manhattan(&(point_t){.x = 1});
}
The big three1 seem to compile it OK, but I couldn't actually find a reference explaining that the lifetime of the temporary will be extended at least through the function call.
0 As it turns out, based on the answer by M.M below, part of my searching issues was because I was looking for information on temporaries, while the correct C term for this particular initialization construct is compound literal.
1 I should call it "the big cross-platform three" really, in deference to MSVC, but actually I really just mean "the C compilers godbolt supports".