By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,694 Members | 1,882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,694 IT Pros & Developers. It's quick & easy.

Appending rows to 2-dim arrays

P: n/a
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.

Example:

int ** someArray;
int * topRow;
int * bottomRow;

&someArray = someFun(); //returns 2D array.

/*
I want to...

someArray = topRow + someArray + bottomRow

*/

Thanks in advance,

Nathan Gilbert
--
"The life of a repoman is always intense."
Dec 7 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Nathan Gilbert wrote:
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[i]...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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Dec 7 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.