Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: wordsmithing

...

Wiki Markup
following initialization, the {{int}} pointer {{ip}} contains the address of an unnamed object of type {{int \[4\]}}, allocated on the stack. Once {{func}} returns, any attempts to access this object will result inproduce undefined behavior.

It should be noted that only one object is created per compound literal -- even if the compound literal appears in a loop and has dynamic initializers (6.5.2.5.16).

...

Wiki Markup
In this noncompliant code example, the programmer mistakenly assumes that the elements of the {{ints}} array of pointer to {{int_struct}} are assigned the addresses of distinct {{int_struct}} objects, one for each integer in the range \[0, MAX_INTS-1\]:

Code Block
bgColor#FFCCCCborderStylesolid
#include <stdio.h>

typedef struct int_struct {
  int x;
} int_struct;

#define MAX_INTS 10

int main(void){
  size_t i;
  int_struct *ints[MAX_INTS];

  for (i = 0; i < MAX_INTS; i++) {
    ints[i] = &(int_struct){i};
  }

  for (i = 0; i < MAX_INTS; i++) {
    printf("%d\n", ints[i]->x);
  }
}

...