1 D array of structs:: how to access/define it??

uCsDNerd

Senior member
Mar 3, 2001
338
0
0
::EDITED:::::::

Okay wait.. nevermind the 4 D array.... how would I make a 1D array of structs?
I've done a typedef of a struct. and i've made a makeArray that uses calloc and returns a pointer to the struct type

i have :: Blah foo = (Blah *) calloc (1024, sizeof(Blah) ) // where Blah is my struct and foo, hopefully an array?

would this allow me to access the array using indexing such as foo[1020] ???
 

MGMorden

Diamond Member
Jul 4, 2000
3,348
0
76
You need to define foo as a pointer:

Blah * foo = (Blah *) malloc (1024 * sizeof(Blah))

and I used malloc() instead of calloc just because I like it better :).
 

MGMorden

Diamond Member
Jul 4, 2000
3,348
0
76
One more thing, if you're using a constant number of spaces like you did here, and easier (and faster) way would be to do

Blah foo[1024];

This allocates the memory on the stack instead of the heap (much faster). The disadvantage is that you can't have the arrary be larger or smaller depending on the data you're putting in it (for example somthing like an image would be like):

pixel * pixelGrid = (pixel *) malloc (width * height * sizeof(pixel));

This allows you to have the array be different sizes depending on what width and height are. Just a FYI thing.