Get address of temporary (compound literal) parameter in C


bee rope

I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 .

Is it legal to take a temporary address constructed in the parameter list of a function call in C99?

For example, like init_listor init_desig_initas 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});
}

Big Three 1 seems to compile it , but I can't actually find a reference to saying that the lifetime of a temporary can be extended at least by a function call.


0 It turns out, based on MM 's answer below , that part of the question I searched for was because I was looking for temporary information , and the correct C term for this particular initialization construct is a compound literal .

1 Compared to MSVC, I should have called it "the big cross-platform trio", but I actually meant "C compiler Godbolt support".

MM

(point_t){1, 2}Not "temporary". This is a compound literal. (The same sequence of tokens in C++ have different meanings and the two languages ​​should not be confused with each other).

Compound literals are lvalues, so it is legal to use unary operators &on them . C11 6.5.2.5/5 covers the storage period:

If the compound literal appears outside the body of the function, the object has static storage duration; otherwise, it has the automatic storage duration associated with the enclosing block.

So this code is correct and the compound literal exists until the end of the function in which it is declared.

Related


Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Compound literal pointer passed as parameter in C

Function Is pass-by-value cast pointer valid in C? If not why does this work? #include <stdio.h> typedef struct { int size; char* str; } MY_STRUCT; void print_my_struct_prt(MY_STRUCT* mstrct) { printf("%s%d%s%s\n", "size: ", mstrct->size, " c

the address of the compound literal used as the interface

Sridhar: When used as an interface, the address of the composed literal is evaluated as the literal itself. Can someone point me to the part of the reference spec that is relevant to this? package main import "fmt" type ntfc interface { rx() int } type c

the address of the compound literal used as the interface

Sridhar: When used as an interface, the address of the composed literal is evaluated as the literal itself. Can someone point me to the part of the reference spec that is relevant to this? package main import "fmt" type ntfc interface { rx() int } type c

the address of the compound literal used as the interface

Sridhar: When used as an interface, the address of the composed literal is evaluated as the literal itself. Can someone point me to the part of the reference spec that is relevant to this? package main import "fmt" type ntfc interface { rx() int } type c

the address of the compound literal used as the interface

Sridhar: When used as an interface, the address of the composed literal is evaluated as the literal itself. Can someone point me to the part of the reference spec that is relevant to this? package main import "fmt" type ntfc interface { rx() int } type c

the address of the compound literal used as the interface

Sridhar: When used as an interface, the address of the composed literal is evaluated as the literal itself. Can someone point me to the part of the reference spec that is relevant to this? package main import "fmt" type ntfc interface { rx() int } type c

the address of the compound literal used as the interface

Sridhar: When used as an interface, the address of the composed literal is evaluated as the literal itself. Can someone point me to the part of the reference spec that is relevant to this? package main import "fmt" type ntfc interface { rx() int } type c

C++ get the address of a temporary object

Ricky I have two code snippets that expect the same result: First: SomeClass somefunc(...){ SomeClass newObj; //some codes modify this object return newObj; } int main(){ SomeClass *p; p = &(somefuc(...)); } the second: SomeClass *somefun

C++ get the address of a temporary object

Ricky I have two code snippets that expect the same result: First: SomeClass somefunc(...){ SomeClass newObj; //some codes modify this object return newObj; } int main(){ SomeClass *p; p = &(somefuc(...)); } the second: SomeClass *somefun

C++ get the address of a temporary object

Ricky I have two code snippets that expect the same result: First: SomeClass somefunc(...){ SomeClass newObj; //some codes modify this object return newObj; } int main(){ SomeClass *p; p = &(somefuc(...)); } the second: SomeClass *somefun

C++ get the address of a temporary object

Ricky I have two code snippets that expect the same result: First: SomeClass somefunc(...){ SomeClass newObj; //some codes modify this object return newObj; } int main(){ SomeClass *p; p = &(somefuc(...)); } the second: SomeClass *somefun

C++ get the address of a temporary object

Ricky I have two code snippets that expect the same result: First: SomeClass somefunc(...){ SomeClass newObj; //some codes modify this object return newObj; } int main(){ SomeClass *p; p = &(somefuc(...)); } the second: SomeClass *somefun

compound literal

I am Islam I ran into a situation in Go and I couldn't find any solution. The problem starts with the following code: graph := chart.BarChart{ Title: "Remote#1 Bar Chart", Background: chart.Style{ Padding: chart.Box{ Top: 40,

Address of string literal in C

Lavnor I've read that string literals are of type char[n+1], where n is the length. Storage of string literals is an implementation issue. But it still has to be unique in an instant. printf ("%u\t %s\t %d\t %c\t %f\t %e\t %x\t %p\t", &"XY",&"XY",&"XY",&

Address of string literal in C

Lavnor I've read that string literals are of type char[n+1], where n is the length. Storage of string literals is an implementation issue. But it still has to be unique in an instant. printf ("%u\t %s\t %d\t %c\t %f\t %e\t %x\t %p\t", &"XY",&"XY",&"XY",&

Address of string literal in C

Lavnor I've read that string literals are of type char[n+1], where n is the length. Storage of string literals is an implementation issue. But it still has to be unique in an instant. printf ("%u\t %s\t %d\t %c\t %f\t %e\t %x\t %p\t", &"XY",&"XY",&"XY",&