Growing Array in C [closed]
Can any one Explain me plz the concept of Growing Array Of Structs. i mean dynamic Arrays. thanks for your Time.
Start with a small array of some size, then whenever you need to increase the size, use realloc
to do so; it is common to double the size of the array whenever you resize it.
For example:
int length = 5;
my_struct *array = NULL;
/* Initialization */
array = (my_struct *)malloc(length * sizeof(my_struct));
/* Use array[0] .. array[length - 1] */
/* When you reach the limit, resize the array */
length *= 2;
array = (my_struct *)realloc(array, length * sizeof(my_struct));
Do you mean dynamic size of an array? In that case you must allocate memory fitting for you needs. See realloc
I have a dynamically growing string buffer implementation. You can get it from here. It uses the same malloc
and realloc
technique.
Look at realloc
function from the standard library.
Look up malloc?
精彩评论