November 2001
Beginner
1128 pages
29h 12m
English
To write a function that has a two-dimensional array as an argument, you need to remember that the name of an array is treated as its address, so the corresponding formal parameter is a pointer, just as for one-dimensional arrays. The tricky part is declaring the pointer correctly. Suppose, for example, we start with this code:
int data[3][4] = { {1,2,3,4}, {9,8,7,6}, {2,4,6 8}};
int total = sum(data, 3);
What should the prototype for sum() look like? And why does the function pass the number of rows (3) as an argument and not also the number of columns (4)?
Well, data is the name of an array with three elements. The first element is, itself, an array of 4 int values. Thus, the type of data is pointer-to-array-of-4- ...
Read now
Unlock full access