What is the best way to progressively fill an array in C? -
suppose have array, defined as
int arr[10];
i want progressively fill array (assuming not run out of memory allocated array), according loop of form
for(int = 0; < some_number; i++) if(some_condition) add_element_to_arr
what best way this? 2 methods can think of 1) using auxiliary variable remember number of stored values, or 2) using pointer same thing.
what preferred/standard/conventional way this?
also, having kind of operations multiple arrays, remember how many values have been added each array 1 has use auxiliary variable each array. preferred way deal this?
you use pos
variable this:
for(int = 0, pos = 0; < some_number && pos < 10; i++) if(some_condition) arr[pos++] = i;
no need use pointer in case.
update:
with multiple arrays (different sizes), create struct pointer array , current position, way both values stay together:
struct arr_data { int *arr; int current_pos; }
and add size maybe:
struct arr_data { int *arr; int size; // or unsigned int or size_t int current_pos; // or unsigned int or size_t }
you create array of structs, depending on implementation.
Comments
Post a Comment