2-Dimensional Arrays
C array
Declaration
#define MAX 20
int numbers[10][10], vals[MAX][3], others[MAX*2 + 3][2*MAX];
float wages[100][20];
char letters[24][4];
int temp_array[5][5] = {{1, 3, 5, 6, 100},
{2, 4, 6, 7, 101},
{3, 5, 7, 8, 102},
{4, 6, 8, 9, 103},
{5, 7, 9, 10, 104}};
int another[100][4] = {0, 1, 3, 6, 9};
Access
Single element
numbers[0][2] = 2;
letters[23][2] = 'c';
wages[135][24] = 5.15; /* is there a problem here */
Whole array
Not possible. You may only access an array 1 element at a time.
another = numbers; Is not a valid statement. What this says is give
the starting address of numbers to the array another. This will not
"copy" the values in numbers into the first elements of another. Don't
even try it.
Passing arrays to functions.
pass_whole_array_in_only(numbers, low_row, high_row, low_col, high_col);
pass_whole_array-inout_out(numbers, low_row, high_row, low_col, high_col);
Receiving arrays to functions
void pass_whole_array_in_only(int array[][10], int lrow, int hrow,
int lcol, int hcol);
or void pass_whole_array_in_only(int [][10], int, int, int, int);
or void pass_whole_array_in_only(int **, int, int, int, int);
/*Don't use the 3rd method unless specifically requested to do so*/
/*With multidimensional arrays, * notation is horrible to deal with*/
void pass_whole_array-inout_out(int array[][10], int lrow, int hrow,
int lcol, int hcol);
or void pass_whole_array-inout_out(int [][10], int, int, int, int);
or void pass_whole_array-inout_out(int **, int, int, int, int);
Accessing arrays passed as parameters is exactly like accessing them
within the scope in which they were declared. (See access examples above.)
Passing part of an array to a function
min = min4(numbers[1][1], numbers[2][1], numbers[3][1], numbers[4][1]);
get_int(&number[1][0]);
fill_1d(numbers[1], 0, 10); /* fills 1 row of 2-d array */
NOTE: min4() prototype and header have not changed at all
get_int() prototype and header have not changed at all
fill_array(int [], int, int) prototye and header have not changed