...
| Wiki Markup |
|---|
In this noncompliant code example, the programmer mistakenly assumes that the elements of the {{ints}} array of the 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 | ||||
|---|---|---|---|---|
| ||||
#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);
}
}
|
...
| Code Block | ||||||
|---|---|---|---|---|---|---|
| ||||||
#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);
}
}
|
...