【问题标题】:Passing a row in a 2D array to a function using a pointer in C [closed]使用C中的指针将二维数组中的一行传递给函数[关闭]
【发布时间】: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


【解决方案1】:

传递每一行的地址:

min = RowMin(a[i], c);

然后在您的例程中执行此操作,以便在每一行中找到 min

 int RowMin(int *prow, int ncols)
    {
        int min=prow[0], i;

        for(i=0; i<ncols; i++)
        {

            if(min > prow[i])
            {
                min = prow[i];

            }    
        }    
        return min;
    }

【讨论】:

    【解决方案2】:

    在这个循环中

    for(i=0;i<r;i++)
    {
        min = RowMin(i, c);
        printf("The min in row %d is %d",i, min);
    }    
    

    您必须将数组的一行传递给函数。所以调用看起来像

        min = RowMin( a[i], c );
    

    考虑到您应该检查输入的行和列的值是否不大于 MAX。或者,如果您的编译器支持可变长度数组,您可以在其大小由输入的 r 和 c 值设置时使用可变长度数组。

    函数本身也是无效的。可以这样写

    int RowMin( const int *prow, int ncols )
    {
        int i = 0;
        int min = prow[i];
    
        while ( ++i < ncols )
        {
            if ( prow[i] < min )
            {
                min = prow[i];
            }    
        }    
    
        return min;
    }
    

    相应地,函数必须声明为

    int RowMin( const int *prow, int c );
    

    【讨论】:

    • 你在这里找到最大值
    • 你的情况不对
    • @Gopi 谢谢。这是一个错字。
    【解决方案3】:

    函数调用应该是这样的

    min = RowMin(&amp;a[i],c)

    你应该传递每一行的地址。

    函数应该这样修改

    int minRow(int *ptr, int columns)
    {
      //ptr has the address of each row
      int minElt = ptr[0]; //Contains the first Element
    
      for(int i = 0; i < columns; ++i)
      {
          if( minElt > ptr[0] )
              minElt = ptr[0]
      }
      return minElt;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-09
      • 1970-01-01
      • 2019-03-28
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2018-12-08
      • 1970-01-01
      相关资源
      最近更新 更多