Nathan said:
I have a function that is returning a 2D array (declared using double pointers)
and I want to be able to append a row of data to the beginning and end of this
array without having to create a new 2D array and iterate through copy contents
from arrays into this new 2D array.
There are no 2D arrays in C; there are arrays of arrays of T,
pointers to arrays of T, pointers to pointers of T and arrays
of pointers to T to emulate 2D arrays.
Do you understand the differences between the following declarations?
T arr[N][M];
T (*arr)[M];
T *arr[N];
T **arr;
You chose the fourth variant but could have chosen the third
as well.
So, you essentially have a pointer to storage containing
one or more pointers to T (or, in case 3, an array containing
pointers to T).
In order to change the order of your rows, or add to the rows,
you change the storage (array) containing the pointers to the
rows:
arr[0] is a pointer to the first row
.....
arr[I-1] is a pointer to the (I)th row
where I is the number of rows.
If arr points to sufficiently large storage (is a sufficiently
large array), say I + i == N, then you can add up to i rows by
copying pointers to the respective rows into a
...a[I+i-1].
In this case, you are responsible for the number of "columns"
per row.
If arr does not point to sufficiently large storage, then you
just have to reallocate the storage for the pointers to the rows
but not the rows themselves.
Example:
int ** someArray;
int * topRow;
int * bottomRow;
&someArray = someFun(); //returns 2D array.
You cannot change the storage location of a variable.
/*
I want to...
someArray = topRow + someArray + bottomRow
*/
See above. Inserting rows can be achieved by memmove()ing
the pointers to the rows which come after the new row(s)
"downwards" as is appropriate.
If you have implemented this and have some problems, just
ask -- but do show us your best shot at it.
Cheers
Michael