Array of C structs -


if create struct in c , want add them array not set fixed size, how array created?

can 1 create tempstruct used on every iteration while getting user input , store in array, using same tempstruct struct in loop?

how array created if size unknown depends on user input, , how structs added array?

when size unknown @ compile time, you'll need allocate memory on heap, rather in data segment (where global variables stored) or on stack (where function parameters , local variables stored). in c, can calling functions malloc.

mystructtype *myarray = (mystructtype *)malloc(numelements * sizeof(mystructtype) ... ... free(myarray) 

if you're actully using c++, it's better use new[] , delete[], e.g.

mystructtype *myarray = new mystructtype[numelements] ... ... delete [] myarray 

note new[] must paired delete[]. if you're allocating single instance, use new , delete (without "[]"). delete[] , delete not equivalent.

also, if you're using c++, it's easier , safer use stl vector.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -