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

Professor Zhou Ligong has worked tirelessly over the years on his book "Programming and Data Structure." The electronic version of the book has been freely distributed to electronic engineers and college students, and it can be read online by replying with "[program design]" on the official WeChat account. After the publication of the content, a learning trend emerged in the electronics field. With Professor Zhou's authorization, this official account has serialized the book’s content and is eager to share it with everyone. The first chapter introduces the basics of programming. This section discusses the use of a two-dimensional array as a function parameter. >>> 1.7.3 Using a Two-Dimensional Array as a Function Parameter Function Prototype When an array of arrays is used as a function parameter, the array name is treated like a pointer. Therefore, the corresponding formal parameters are also pointers, similar to those in a one-dimensional array. But how do you correctly declare a pointer variable pData that points to a two-dimensional array? It's not enough to declare pData as pointing to an int type because a pointer to an int can only match data[0]. Suppose you have the following code: int data[3][2] = {{1, 2}, {3, 4}, {5, 6}}; int total = sum(data, 3); What would the prototype for the sum() function look like? Since the array name data can be interpreted as a pointer, its type is int (*)[2], which points to an int [2]. Therefore, pData must be declared with the same type so that data can be passed as an argument to sum(). The function prototype would be: int sum(int (*pData)[2], int size); Alternatively, you could write the prototype as: int sum(int data[3][2], int size); There's also a more readable format, where you only need to specify the second dimension when declaring a function that takes a two-dimensional array as a parameter: int sum(int data[][2], int size); Here, data[] is an implicit declaration of the array pointer, while (*pData) is an explicit declaration. Although data is described as "an array of 2 int values (the number of elements is unknown)," it can also be interpreted as "a pointer to int [2]." So the prototype can also be written as: int sum(int (*pData)[2], int size); Because the subscript is part of the array type, if the second square bracket is empty, the array type becomes incomplete since the compiler doesn't know how to complete it. Thus, the following statements are incorrect: int sum(int data[3][], int size); int sum(int data[][], int size); Why does the sum() function take the number of rows (3) as a parameter instead of the number of columns (2)? Because the prototypes all indicate that data is not an array of pointers. Since data is an array of 2 int values, the number of columns is specified at the time of declaration, which is why the number of columns is not passed as a separate argument. For example: int data[80][3]; int total = sum(data, 20); int total = sum(data + 5, 10); You can also let a function treat a two-dimensional array as a one-dimensional array. For instance, to find the largest element in a two-dimensional array, the function prototype (in iMax.h) might look like: int iMax(int *pData, size_t numData); If you pass the address of the array data as the first argument to the iMax() function, and the total number of elements in the array (row × col) as the second argument: largest = iMax(data, row × col); This won’t compile because the type of data is int (*)[col], but the iMax function expects an int *. The correct way to call it is: largest = iMax(data[0], row × col); data[0] points to the first element of row 0. After compilation, its type is int *, matching the expected argument type. You can also force data to (int *) data to 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 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 means you can point to the first element of data like this: int *ptr = &data[0][0]; int *ptr = data[0]; If you want to represent a year of work using a "two-dimensional array," you could use: int working_time[12][31]; To calculate wages based on one month’s working time, you can pass a specific month’s working time to a function like this: calc_salary(working_time[month]); The corresponding function prototype would be: int calc_salary(int *working_time); This technique is only possible with "array of arrays," while multidimensional arrays fall short. >>> 2. Rows of Two-Dimensional Arrays Since C stores two-dimensional arrays in row-major order—meaning the elements of row 0 are stored first, followed by row 1, and so on—you can access each element by starting with data[0][0], changing the row with a for loop, and the column with another for loop, as shown in Listing 1.34. Program List 1.34: Find Elements 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; This makes pData point to the first row of data. When you add an integer to pData, the integer value is adjusted according to the length of the two integers before the addition, allowing the pointer to move through the array row by row. For each row value, the inner loop iterates through all column values. If you consider a two-dimensional array as a one-dimensional array, you can replace the double loop with a single loop. For example, to 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]. Each increment moves ptr to the next element, eventually reaching the last element of the array. How do you access elements in a specific row of a two-dimensional array? If you want to access the elements one by one instead of moving through the array row by row, use a pointer variable again. To access the elements of the i-th row, initialize ptr to point to the first element of that row: ptr = &data[i][0]; Since data[i] is equivalent to *(data + i), &data[i][0] is equivalent to &(*data[i]), which simplifies to data[i]. You can write this as: ptr = data[i]; The following loop clears the i-th row of the array data: int data[row][col]; for(ptr = data[i]; ptr < data[i] + col; ptr++) *ptr = 0; Since data[i] is a pointer to the i-th row of the array, it can be passed to a function that expects a one-dimensional array. This means functions that operate on one-dimensional arrays can also process a row from a two-dimensional array. For example, the iMax function that finds the largest element in a one-dimensional array can also determine the largest element in the i-th row of a two-dimensional array: largest = iMax(data[i], col); >>> 3. Columns of Two-Dimensional Arrays Dealing with elements in a column of a two-dimensional array is more complex because arrays are stored in row-major order. The following loop sets the i-th column of the array data to zero: 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 array of length col, and pData++ moves it to the start of the next row. In the expression (*pData)[i], *pData represents the entire row, so (*pData)[i] selects the element in the i-th column of that row. Note that parentheses are required around *pData, otherwise the compiler will interpret pData as an array of pointers rather than a pointer to an array. As you can see, understanding the "three elements of a variable"—its type, value, and address—is key to solving any problem related to arrays and pointers.

High Speed Connector

High Speed 6PIN Female Connecor for Cable,High Speed GT32 4 PIN Female Connector,High Speed Connetor

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

Posted on