开发者

Growing Array in C [closed]

开发者 https://www.devze.com 2023-01-30 03:31 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonabl开发者_开发技巧y answered in its current form.
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

精彩评论

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

关注公众号