使用行指针方法输出二维数组中最大的元素及所在的行列
时间: 2024-02-24 22:56:41 浏览: 34
好的,以下是使用行指针方法输出二维数组中最大的元素及所在的行列的代码实现:
```c
#include <stdio.h>
#define ROWS 3
#define COLS 4
void findMax(int (*arr)[COLS], int *max, int *row, int *col) {
*max = arr[0][0];
*row = 0;
*col = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (arr[i][j] > *max) {
*max = arr[i][j];
*row = i;
*col = j;
}
}
}
}
int main() {
int arr[ROWS][COLS] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int max, row, col;
findMax(arr, &max, &row, &col);
printf("Max: %d\nRow: %d\nCol: %d\n", max, row, col);
return 0;
}
```
这里使用了 `int (*arr)[COLS]` 来定义一个指向二维数组的行指针,然后在 `findMax` 函数中使用指针来接收最大值、行和列的值。在 `findMax` 函数中,使用两个嵌套的 for 循环来遍历整个二维数组并找到最大值,然后通过指针来更新最大值、行和列的值。最后在 `main` 函数中输出最大值、行和列的值。