...
If an attempt is made to modify the result of a function call or to access it after the next sequence point, the behavior is undefined.
C forbids functions from to returning arrays; however it is possible to return structs or unions that contain arrays.
...
- In C, the lifetime of a return value ends at the next sequence point. Consequently by the time
printf()is called, thestructreturned by theaddressee()call is no longer considered valid, and may have been overwritten. - C function arguments are passed by value. As a result, copies are made of all objects generated by the arguments. For example, a copy is made of the pointer to
"Hello, %s!\n". Under most circumstances, these copies protect one from the effects of sequence points described earlier. - Finally, C implicitly converts arrays to pointers when passing them as function arguments. This means that in the previous point, a copy is made of the pointer to the
addresee().aarray, and that pointer copy is passed toprintf(). But the array data itself is not copied, and may no longer exists exist whenprintf()is called.
Consequently when printf() tries to dereference the pointer passed as its 2nd argument, it will likely find garbage.
...
Compliant Solution
This compliant solution does not have undefined behavior because store the structure returned by the call to addressee() is stored as the variable my_x before calling the printf() function.
...
Attempting to access or modify an array inside the result of within a function call after a subsequent sequence point may result in unexpected and perhaps unintended program behavior.
...