language: C99 strict (gcc-4.7.2)
date: 187 days 0 hours ago
link:
visibility: private
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdlib.h>
 
struct thing {
    int part1;
    double *part2;
};
 
struct thing *setValue (struct thing *t, int pos, double set){
    if (t->part1 < pos){ // check to see if array is large enough
        t->part2 = realloc (t->part2, (pos+1) * sizeof(double)); /* assume no error */
        for (int a = t->part1 + 1; a < pos + 1; a++)
            t->part2[a] = 0;
        t->part1 = pos;
    }
    t->part2[pos] = set; // ALWAYS stores an integer, don't know why
    return t;
}
 
int main(void) {
  struct thing *abc;
  abc = malloc(sizeof(struct thing)); /* assume no error */
  abc->part1 = 0;
  abc->part2 = malloc(sizeof(double)); /* assume no error */
  setValue(abc, 42, 4.2);
  setValue(abc, 4, -0.04);
  printf("abc's values: %f and %f\n", abc->part2[4], abc->part2[42]);
  return 0;
}