二维数组每行的最大值
时间: 2023-12-26 22:27:37 浏览: 128
以下是使用指针变量查找并输出二维整型数组中每一行的最大值的示例代码:
```c
#include <stdio.h>
void findMax(int *arr, int rows, int cols) {
for (int i = 0; i < rows; i++) {
int max = *(arr + i * cols);
for (int j = 1; j < cols; j++) {
if (*(arr + i * cols + j) > max) {
max = *(arr + i * cols + j);
}
}
printf("Max value in row %d: %d\n", i+1, max);
}
}
int main() {
int arr[4][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
int rows = sizeof(arr) / sizeof(arr[0]); int cols = sizeof(arr[0]) / sizeof(arr[0][0]);
findMax(&arr[0][0], rows, cols);
return 0;
}
```
输出结果为:
```
Max value in row 1: 4
Max value in row 2: 8
Max value in row 3: 12
Max value in row 4: 16
```
阅读全文