C Dynamic List Troubles -


typedef struct {     long blarg; } item;  typedef struct  {     item* items;     int size; } list; 

list , item structs, simple.

list l; l.size = 3; realloc(l.items, l.size*sizeof(item)); 

create list, allocate hold 3 items.

item thing; item thing2; item thing3;  thing.blarg = 1337; thing2.blarg = 33; thing3.blarg = 123;  l.items[0] = thing; l.items[sizeof(item)+1] = thing2; l.items[(sizeof(item)*2)+1] = thing3; 

create items , add them list... when printing them:

printf("list 0: %ld\n", l.items[0].blarg); printf("list 1: %ld\n", l.items[sizeof(item)+1].blarg); printf("list 2: %ld\n", l.items[(sizeof(item)*2)+1].blarg);  list 0: 1337  list 1: 33 { list 2: 1953720652 ! 

where did go wrong?

you should change l.items[sizeof(item)+1] , l.items[(sizeof(item)*2)+1] --> l.items[1] , l.items[2]


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -