Two-dimensional arrays as function parameters are passed in practical applications

Professor Zhou Ligong has dedicated several years to his work on "Programming and Data Structure." The electronic version of the book has been freely distributed to electronic engineers and college groups. Readers can access it online by replying with "[program design]" on the official account. After the release of the book, a wave of learning enthusiasm swept through the electronics community. With Professor Zhou's authorization, this public account has serialized the content of the book and is eager to share it with everyone. The first chapter lays the foundation for programming. This article discusses the use of 1.7.3 as a function parameter. >>> **1.7.3 Using a Two-Dimensional Array as a Function Parameter** > > **1. Function Prototype** When a two-dimensional array is used as a function parameter, the array name is treated as an address, just like a one-dimensional array. However, declaring a pointer that correctly points to an array of arrays is more complex. For example, if you have the following code: ```c int data[3][2] = {{1, 2}, {3, 4}, {5, 6}}; int total = sum(data, 3); ``` What should the function prototype for `sum()` look like? Since the array name `data` can be interpreted as a pointer, its type is `int (*)[2]`, which points to an array of two integers. Therefore, the function prototype should be: ```c int sum(int (*pData)[2], int size); ``` Alternatively, it can also be written as: ```c int sum(int data[3][2], int size); ``` Or even: ```c int sum(int data[][2], int size); ``` In this case, `data[]` is an implicit declaration of a pointer to an array of two integers. Although the number of rows is not specified in the second form, it is still valid because the compiler knows how many elements are in each row. It’s important to note that the second dimension must be specified in the function prototype. If you write: ```c int sum(int data[3][], int size); int sum(int data[][], int size); ``` This will result in a compilation error, as the compiler cannot determine the size of the second dimension. Why does the `sum()` function take the number of rows (3) as a parameter instead of the number of columns (2)? Because the array is declared with a fixed number of columns, the compiler already knows the size of each row. So, the number of columns doesn't need to be passed separately. For example: ```c int data[80][3]; int total = sum(data, 20); int total = sum(data + 5, 10); ``` You can also treat a two-dimensional array as a one-dimensional array when necessary. For instance, to find the largest element in a two-dimensional array, you can pass the array as a one-dimensional pointer. The function prototype could look like this: ```c int iMax(int *pData, size_t numData); ``` To call this function, you would pass the address of the array and the total number of elements: ```c largest = iMax(data, row * col); ``` However, this won’t compile because `data` is of type `int (*)[col]`, while the function expects `int *`. To fix this, you can pass `data[0]` instead: ```c largest = iMax(data[0], row * col); ``` `data[0]` is a pointer to the first element of the first row, and its type is `int *`, which matches the expected argument type. Listing 1.33 shows an example of how to find the maximum value in a two-dimensional array using this approach. Program List 1.33: Find the Maximum Value in a Two-Dimensional Array ```c #include #include "iMax.h" int main(int argc, char *argv[]) { int data[][2] = {{1, 2}, {3, 4}, {5, 6}}; int n = sizeof(data) / sizeof(data[0][0]); printf("%d", iMax((int *)data, n)); return 0; } ``` Since `data[0][0]` is an `int`, the type of `&data[0][0]` is `int *const`. You can point to the first element of `data` in the following ways: ```c int *ptr = &data[0][0]; int *ptr = data[0]; ``` If you want to represent a year of work using a "two-dimensional array," you might do something like this: ```c int working_time[12][31]; ``` Then, to calculate the salary for a specific month, you can pass the corresponding row to a function: ```c calc_salary(working_time[month]); ``` The function prototype would be: ```c int calc_salary(int *working_time); ``` This technique works only with "arrays of arrays," and multidimensional arrays alone are not sufficient. >>> **2. Rows of Two-Dimensional Arrays** In C, two-dimensional arrays are stored in row-major order, meaning that the elements of the first row are stored first, followed by the elements of the second row, and so on. To access each element, you typically use nested loops: one for the row and another for the column. Listing 1.34 demonstrates this approach. Program List 1.34: Find the Elements in a Two-Dimensional Array ```c int sum(int (*pData)[2], int size) { int total = 0; for (int row = 0; row < size; row++) { for (int col = 0; col < 2; col++) { total += pData[row][col]; } } return total; } ``` When initializing a pointer to an array: ```c int (*pData)[2] = data; ``` `pData` now points to the first row of `data`. When you add an integer to `pData`, it adjusts based on the size of the two integers, allowing you to move through the array row by row. If you treat a two-dimensional array as a one-dimensional array, you can replace the nested loops with a single loop. For example, to initialize all elements of a two-dimensional array to zero: ```c for (int *ptr = &data[0][0]; ptr <= &data[row - 1][col - 1]; ptr++) { *ptr = 0; } ``` This loop starts at `data[0][0]` and increments the pointer until it reaches the last element of the array. To access the elements of a specific row, you can initialize a pointer to the start of that row: ```c ptr = &data[i][0]; ``` Or simply: ```c ptr = data[i]; ``` This allows you to loop through the elements of a single row. For example: ```c for (ptr = data[i]; ptr < data[i] + col; ptr++) { *ptr = 0; } ``` Since `data[i]` is a pointer to the i-th row, it can be passed to functions that expect a one-dimensional array. This means that a function like `iMax`, which finds the largest element in a one-dimensional array, can also be used to find the largest element in a specific row of a two-dimensional array: ```c largest = iMax(data[i], col); ``` >>> **3. Columns of Two-Dimensional Arrays** Accessing elements in a column is more complex than accessing elements in a row, since arrays are stored in row-major order. To clear the i-th column of a two-dimensional array, you can use the following loop: ```c int data[row][col], (*pData)[col], i; for (pData = &data[0]; pData < &data[row]; pData++) { (*pData)[i] = 0; } ``` Here, `pData` is a pointer to an array of length `col`, and `pData++` moves to the next row. In the expression `(*pData)[i]`, `*pData` represents the entire row, and `[i]` selects the element in the i-th column. Note that parentheses are necessary to ensure the correct interpretation. As long as you understand the three fundamental aspects of variables—type, value, and address—you can solve any problem involving arrays and pointers.

RF Connection Cables

Rf Connection Cables,Waterproof Sma Connector,Sma F Connector,Sma Connector Types

Dongguan Zhuoyuexin Automotive Electronics Co.,Ltd , https://www.zyx-fakra.com

Posted on