I have an array of pointers to N arrays (see my earlier post). I happy with the assignment of these things and I can access the elements of these arrays with ease. Fantastic.
However, I am now required to modify these arrays, specifically I need to add elements to these arrays. This is done in the form of a function which has an integer, "i" say, passed to it. A random number is generated, call it x for example.
Code:
int *array[]={N0, .... , Ni, Nj, Nk, ... , NN};
void add_to_array(int i)
{
int x;
x=rand()
/*How do I add x to array Ni???*/
return;
}
If it helps I also have an array of length N where the i^th element comprises the number of elements in the i^th array. (If I refer to this particular array again, I will call it "noe"...)
My current approach is to copy the existing array (array[i] using the code above), free it and then increase noe[i] by 1, then malloc a new Ni, fill it from the copy and stick x in at the end. However, I need to do this multiple times! I get a gut feeling that this will cause a problem... Using the most simplistic approach and calling the new array "new" then surely I cannot call the function more than once for fearing of really confusing things??!?
I hope this is reasonably clear. I would rather not post the code I am actually working on or give too much of the context away because I am not looking to have code written for me... merely an approach suggested to help me on my way.
Matt