【发布时间】:2015-01-29 05:28:17
【问题描述】:
我需要编写一个程序,扫描二维数组,然后使用函数在每一行中找到最小值。
说明中给出了原型int RowMin(int *prow, int ncols)。我的问题是我不确定如何使用指针。这是我最好的猜测,它不正确。输入数组的值后程序崩溃。
这是我目前所拥有的:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int RowMin(int *prow, int c);
int main()
{
int a[MAX][MAX];
int r, c, min;
int i = 0, j = 0;
printf("Enter number of rows & columns of array:\n");
scanf("%d %d", &r, &c);
printf("\nEnter elements of 2-D array:\n");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf("%d", &a[i][j]);
}
}
for(i=0;i<r;i++)
{
min = RowMin(i, c);
printf("The min in row %d is %d",i, min);
}
return 0;
}
int RowMin(int *prow, int ncols)
{
int temp, i;
int a[*prow][ncols]; //this is where it is messing up
temp = a[*prow][0];
for(i=0; i<ncols; i++)
{
printf("Good5");
if(temp > a[*prow][i])
{
temp = a[*prow][i];
}
}
return temp;
}
【问题讨论】:
-
请更具体。
-
您传递的是 int (
i) 而不是指针。 -
请在所有警告设置为开启的情况下进行编译,修复所有警告,然后,如果问题仍然存在,请最终返回此处:您将
int传递给函数,您应该在其中传递 @ 987654325@,你使用a[*pow][0]越界,你使用a[*pow][0]而不初始化它......
标签: c function pointers prototype multidimensional-array