Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: removed sort of pointless example and other changes

...

A non-lvalue expression with structure or union type, where the structure or union contains a member with array type (including, recursively, members of all contained structures and unions) refers to an object with automatic storage duration and temporary lifetime. Its lifetime begins when the expression is evaluated and its initial value is the value of the expression. Its lifetime ends when the evaluation of the containing full expression or full declarator ends. Any attempt to modify an object with temporary lifetime results in undefined behavior.

...

C functions may not return entire arrays; however, functions can return a pointer to an array, and return structs or unions that contain arrays. Consequently, if a function call's return value contains an array, that array should never be modified within the expression containing the function call. In C99, this array should also never be accessed.

Noncompliant Code Example (C11)

The following noncompliant C11 code attempts to use the indeterminate value of the pointer p. The following is compliant C99 code:

Code Block
bgColor#FFCCCC
langc
#include <stdio.h>

struct X { int a[5]; } f();
int *p = f().a;
printf("%p\n", p);

Compliant Code Example (C11)

  Do not access an array returned by a function after the next sequence point  or after the evaluation of the containing full expression or full declarator ends.

Nonompliant Code Example

This noncompliant code example The following code is C11 compliant; however it is noncompliant C99 code having undefined behavior because the sequence point preceding the call to printf comes between the call and the access by printf of the string in the returned object:

Code Block
bgColor#FFCCCC
langc
#include <stdio.h>

struct X { char a[8]; };

struct X salutation() {
  struct X result = { "Hello" };
  return result;
}

struct X addressee() {
  struct X result = { "world" };
  return result;
}

int main(void) {
  printf("%s, %s!\n", salutation().a, addressee().a);
  return 0;
}

Noncompliant Code Example (C11)

The following noncompliant code attempts to retrieve an array and increment the first value from a struct that is returned by a function call. Because the array is being modified, the attempted retrieval results in undefined behavior.

...