>>>>  1.7.3 Using a two-dimensional array as a function parameter >>> 1.  Function prototype When an array of arrays is used as a function parameter, the array name is also treated as an address, so the corresponding formal parameters are also pointers like the same dimension array. It is more difficult to correctly declare a pointer variable pData pointing to an array of array data? It is not enough to declare pData as pointing to an int type, because a pointer variable pointing to an int type can only match the type of data[0]. Suppose you have the following code: Int data[3][2] = {{1, 2}, {3, 4}, {5, 6}}; Int total = sum(data, 3); So what is the prototype of the sum() function? Since the array name data in the expression can be interpreted as a pointer, that is, the type of data is a pointer type int (*)[2] pointing to int [2], pData must be declared as a matching type, and data can be used as The argument is passed to sum(). Its function prototype is as follows: Int sum(int (*pDdata)[2], int size); Of course, you can also write this function prototype in the following form: Int sum(int data[3][2], int size); There is also a format that is exactly the same as the above prototype, but more readable. When declaring a function that takes a two-dimensional array as a parameter, just provide the second one: Int sum(int data[][2], int size); Among them, the data[] expression is an implicit declaration of the array pointer, and the (*pData) expression is an explicit declaration of the pointer. Although data is "an array of 2 int values ​​(the number of elements is unknown)", it can also be interpreted as "a pointer to int [2]". which is: Int sum(int (*pData)[2], int size); Since the subscript is part of the array type, if the second square bracket is empty, the array type is incomplete because the compiler does not know how to complete it. So a statement like this: Int sum(int data[3][], int size); Int sum(int data[][], int size); it is wrong. Why does the sun() function take the number of rows (3) as a parameter instead of using column (2) as a parameter? The above prototypes all point out that data is not an array of pointers. Since data is an array of 2 int values, it means that the number of columns is specified at the time of declaration, which is why the number of columns is not passed as a separate function argument. such as: Int data[80][3]; Int total = sum(data, 20); Int total = sum(data+5, 10); Of course, you can also let a function treat a two-dimensional array as a one-dimensional array, such as how to find the largest element in a two-dimensional array. Its function prototype (iMax.h file) is as follows: Int iMax(int ​​*pData, size_t numData) If the address data of the array is taken as the first argument of the iMax() function, the total number of elements in the array data, row*col, is taken as the second argument: Largest = iMax(data, row*col); It cannot be compiled because the type of data is int (*)[col], and the expected argument type of the iMax function is int *. The correct form of invocation is as follows: Largest = iMax(data[0], row*col); Data[0] points to element 0 of line 0. After the compiler converts, its type is int *, and the actual participating parameter types are the same. When forcing data to (int *) data, you can also find the maximum value of the elements in the two-dimensional array, as shown in Listing 1.33. Program list  1.33  Find the maximum value of the sample in a two-dimensional array 1 #include 2 #include "iMax.h" 3 4 int main(int argc, char *argv[]) 5 { 6 int data[][2] = {{1, 2}, {3, 4}, {5, 6}}; 7 int n = sizeof(data) / sizeof(data[0][0]); 8 printf("%d", iMax((int *)data, n)); 9 return 0; 10 } Since data[0][0] is an int value, the type of &data[0][0] is int *const. That is, you can point to the first element of data in the following way, increasing the value of the pointer so that it points to the next element. which is: Int *ptr = &data[0][0]; Int *ptr = data[0]; If you want someone to work for a year, use the following "array of arrays" to indicate: Int working_time[12][31]; Here, if you develop a function that calculates wages based on one month's working time, you can pass a certain month's working time to this function as follows: Calc_salary(working_time[month]); The corresponding function prototype is as follows: Int calc_salary(int *working_time); This technique can only be achieved with "array of arrays", while multidimensional arrays are pale and powerless. >>>  2. Rows of two-dimensional arrays Since the C language stores two-dimensional arrays in the main sequence of rows, that is, the elements of 0 rows are stored first, then the elements of 1 row are stored, and so on. So to access each element in the array, start with data[0][0], change the row with a for loop, and change the column with another for loop, as shown in Listing 1.34. Program list  1.34  Find the elements and sample programs in a two-dimensional array 1 int sum(int (*pData)[2], int size) 2 { 3 int total = 0; 4 5 for(int row = 0; row < size; row++) 6 for(int col = 0; col < 2; col++) 7 total += pData[row][col]; 8 return total; 9 } When initializing data with a pointer to an array: Int (*pData)[2] = data; It causes pData to point to the first row of data. When pData is added to an integer, the integer value is first adjusted according to the length of the two integer values, and then the addition is performed, so this pointer can be used to move in data one by one. For each row value, the inner for loop will iterate through all the col values. If you look at a two-dimensional array as a one-dimensional array, the double loop described above can be changed to a single loop. For example, initialize all elements of a two-dimensional array to 0: For(int *ptr = &data[0][0]; ptr <= &data[row - 1][col - 1]; ptr++) *ptr = 0; When the loop starts, ptr points to data[0][0], ptr++ causes ptr to point to data[0][1], data[0][2]...when ptr reaches data[0][col-1] (ie When the last element of line 0), the ptr is incremented again to point to data[1][0], and the process continues until ptr crosses data[row-1][col-1] (the last one in the array) Element) so far. How to deal with elements in a row of a two-dimensional array? If you need a pointer to access the elements of the array one by one instead of moving around the array row by row, choose to use the pointer variable ptr again. In order to access the elements of the i-th row, ptr needs to be initialized to point to element 0 of the i-th row in the array data. which is: Ptr = &data[i][0]; Since data[i] is equivalent to *(data + i), &data[i][0] is equivalent to &(*(data[i]+0)), which is equivalent to &*data[i]. Since the & operator can be offset, it is equivalent to data[i], which can be abbreviated as: "ptr = &data[i][0];" Ptr = data[i]; The following loop clears the ith row of the array data, which is used for this simplification. which is: Int data[row][col]; For(ptr = data[i]; ptr < data[i] + col; ptr++) *pData = 0; Since data[i] is a pointer to the i-th row of the array data, data[i] is passed to a function that requires a one-dimensional array as an argument, that is, a function that uses a one-dimensional array can also use a row in a two-dimensional array. . Obviously, the iMax function that finds the largest element in a one-dimensional array can also be used to determine the largest element of the i-th row in the two-dimensional array data: Largest = iMax(data[i], col); >>>  3. Two-dimensional array of columns Since arrays are stored in rows rather than columns, dealing with elements in a column of a two-dimensional array is relatively more complicated. The following loop is cleared to the ith column of the array data: Int data[row][col], (*pData)[col], i; For(pData = &data[0]; pData < &data[row]; pData++) (*pData)[i] = 0; Here, pData is declared as a pointer to an integer array of length col, and pData++ moves pData to the beginning of the next line. In the expression (*pData)[i], *pData represents an entire line of data, so (*pData)[i] selects the element in the i-th column of the row. Note that *pData must use parentheses, otherwise the compiler will consider pData to be an array of pointers, not a pointer to an array. It can be seen that as long as you grasp the "three elements of the variable (that is, the type of the variable, the value of the variable, and the address of the variable)" and throughout, all problems will be solved.
This is a special TOVAPO Z001-1500 Vape Pen product series. We sell Tovapo Z001-1500 vape pen disposable,Z001-1500 vape pen kit, Z001-1500 vape pen disposable,Z001-1500 vape pen kit,Z001-1500 vape starter kit,850mah vape pen,850 mah vape disposable Ningbo Autrends International Trade Co.,Ltd. , https://www.supervapebar.com
Z001-1500 vape starter kit, and other Tovapo vapes.
We are specialized electronic cigarette manufacturers from China, Vapes For Smoking, Vape Pen Kits suppliers/factory, wholesale high-quality
products of Modern E-Cigarette R & D and manufacturing, we have the perfect after-sales service and technical support. Look forward to
your cooperation!
The first chapter is the basis of programming. This article uses 1.7.3 as a function parameter.