Originally Posted by
algorism
int **first_array implies that it has a dynamic number of columns (not necessaily a "ragged" array, but a fixed number of columns that is only able to be determined at runtime) as well as a dynamic number of rows. If it has a fixed number of columns determinable at compile time, then it should be:
Code:
int (*first_array)[NUM_COLS];
Note that NUM_COLS is a better name for a fixed number of columns. MAX_COLS implies that it could be less. Your print routine assumes that it always uses "MAX_COLS" columns.
If the number of columns is dynamic then your print function will not work properly in general.
thanks for reply.
I figured out, that normally, my second dimension was always fixed and I could always workout the size of first dimension. Therefore, I started to drop the pointer notation, and instead defining arrays as:
Code:
#define NUM_COLS 4
#define MAX_ROWS 13
int first_array[MAX_ROWS][NUM_COLS]
As noted, the first dimension can go to the maximum of 13, but it can be smaller.
I have a follow up question, with such notation, if I want to copy contents between two 2d arrays, is the following way using "memcpy" correct.
Code:
int p[MAX_ROWS][MAX_COLS];
int arr[3][4] = {
{1,2,3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
memcpy(p,arr,sizeof(int)*3*4);
And, also to use memcmp function for comparing the contents of the two arrays.
Code:
int array[2][3] = // some contents;
int array2[2][3]=// some contents;
memcmp(array, array2, sizeof(array));
thanks,