Heyo. In my C program, im dealing with lots of operations in which i have to read files, and store its data in arrays. For that, as the array handling in C is a little bit complicated, i'm using the following code suggested in this post:C dynamically growing array
typedef struct {
float *array;
size_t used;
size_t size;
} points;
void initArrayInd(arrayInd *a, size_t initialSize) {
a->array = (GLubyte *)malloc(initialSize * sizeof(GLubyte));
a->used = 0;
a->size = initialSize;
}
void insertArrayInd(arrayInd *a, GLubyte element) {
if (a->used == a->size) {
a->size *= 2;
a->array = (GLubyte *)realloc(a->array, a->size * sizeof(GLubyte));
}
a->array[a->used++] = element;
}
void freeArrayInd(arrayInd *a) {
free(a->array);
a->array = NULL;
a->used = a->size = 0;
}
I'm used to Java programming, so my way of thinking how should i code the things is, in most of the cases, wrong. I want to, based on this dynamic array allocation, be able to create a new function that will insert me a record in the position i have specified. I want to know what is the best way to do so.
Should i insert the the new record at the end of the array, and then shift everything one position. Should i create a new array, copy the i-1 elements, then place i and copy the i+n elements. Should i split the initial array into two, create a third and mix everything together. What is the best way in C to accomplish so?
EDIT: This would do it?
void insertFPointsAt(points *a, float element, int position) {
if (a->used == a->size) {
a->size *= 2;
a->array = (float *)realloc(a->array, a->size * sizeof(float));
}
memmove(&a->array[position], &a->array[position+1], &a->array[a->used] - &a->array[position]);
a->array[position] = element;
}