开发者

Growing Array in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonabl开发者_开发技巧y answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜