Is it legal to access a field of a returned union without a variable?

<>

This question already has an answer here:

Answers


This is valid and allowed by c standard and there is no undefined behavior in this code.

EDIT: For the snippet

 int main(void)
 {
     printf(" thinga.integ=%d .arr=%s\n", f("1").integ, f("1").arr);
 }  

f("1").arr is referencing arr element of the union. Since arr is an array and as per C rule, in this context array will decay to pointer to its first element. Since t is local to the function (an automatic local variable and will no longer be exist once function return), accessing arr elements will invoke undefined behavior.


This is just identical to

printf("thinga is %d\n", (union thinga) {.integ = 1}.integ);

, which is obviously well-defined.


Need Your Help

Assigning output of a command to a variable(BASH)

linux bash shell command

I need to assign the output of a command to a variable. The command I tried is: