繁体   English   中英

指针,function和c程序中的二维数组

[英]Pointers, function and 2d array in c program

我尝试制作一个程序来显示二维数组列中的最大数字,但是这个程序有一个问题,无法产生所需的结果。 在我的编译器中说:

 3  5   [Note] expected 'int *' but argument is of type 'int (*)[(sizetype)(b)]'

这是我的代码:

    #include<stdio.h>
    
    int array2d(int *x, int a, int b){
        int i,j;
        int max[j];
        for(i=0; i<a ; i++){
            max[j]= *x;
            for(j=0; j<b ; j++){
                if(*x>max[j])
                {
                    max[j]=*x;
                }
            }
            printf("\nthe maximum value of each column is : %d", max[j]);
        
    }
    int main(){
        int a,b,i,j;
        
        printf("enter the size of array (rows) & (column) : ");
        scanf("%d %d",&a,&b);
        
        int x[a][b];
        
        printf("enter the number : ");
        for ( i = 0; i < a; i++)
        {
            for ( j = 0; j < b; j++)
            {
                scanf("%d",&x[i][j]);
            }
        }
        
        array2d(x,a,b);
        
        return 0;
    }

程序输入:

4 4

并输入号码:

1 3 2 1
8 4 3 2
1 2 3 4
9 8 7 6

并期望这个 output:

9 8 7 6

我应该怎么做才能解决它? 我需要您的意见,也许有人想帮助我编写正确的代码。

这里有一个工作示例:

请参阅指针算法。

void printArray(int *x, size_t a, size_t b)
{
    if(x && a && b)
    {
        for(size_t i = 0; i < a; i++)
        {
            for(size_t j = 0; j < b; j++)
                printf("%d\t", *(x + i * a + j));
            printf("\n");
        }
    }
}

void array2d(int *x, size_t a, size_t b)
{
    if(x && a && b)
    {
        for(size_t i = 0; i < b; i++)
        {
            int max = *(x + i);
            for(size_t j = 0; j < a; j++)
            {
                if(*(x + j * a + i) > max)
                {
                    max = *(x + j * a + i);
                }
            }
            printf("\nthe maximum value of each column is : %d", max);
        }
    }

}

int main(void)
{
    size_t a = 0,b = 0;
    
    printf("enter the size of array (rows) & (column) : ");
    scanf("%d %d",&a,&b);
    printf("\n");
    

    int x[a][b];

    srand(time(NULL));
    
    for (size_t i = 0; i < a; i++)
    {
        for (size_t j = 0; j < b; j++)
        {
            x[i][j] = rand() % 1000;
        }
    }
    
    printArray(*x, a, b);
    array2d(*x, a, b);
    
    return 0;
}

https://godbolt.org/z/zzaxG6

您声明了一个二维可变长度数组

    scanf("%d %d",&a,&b);
    
    int x[a][b];

数组的元素类型是int[b]

在表达式中使用的数组指示符在极少数情况下被转换为指向其第一个元素的指针。

所以在这个 function 电话中

array2d(x,a,b);

数组指示符x被转换为指向其类型为int ( * )[b]的第一个元素的指针。 但是相应的 function 参数的类型为int *

int array2d(int *x, int a, int b){

并且没有从类型int ( * )[b]到类型int *的隐式转换。 所以编译器会报错。

此外,在 function 中,您正在使用另一个可变长度数组max ,其大小由未初始化的变量j设置。

    int i,j;
    int max[j];

无需将此数组声明为最大值 output。

此外,function 的返回类型为int ,但不返回任何内容。

所以在任何情况下 function 都是不正确的,没有意义。

可以通过以下方式声明和定义 function

void array2d( size_t m, size_t n, int a[m][n] )
{
    for ( size_t j = 0; j < n; j++ )
    {
        int max = a[0][j];

        for ( size_t i = 1; i < m; i++ )
        {
            if ( max < a[i][j] ) max = a[i][j];
        }

        printf( "\nthe maximum value in the column %zu is: %d", j, max );
    }
}

这是一个演示程序。

#include <stdio.h>

void array2d( size_t m, size_t n, int a[m][n] )
{
    for ( size_t j = 0; j < n; j++ )
    {
        int max = a[0][j];

        for ( size_t i = 1; i < m; i++ )
        {
            if ( max < a[i][j] ) max = a[i][j];
        }

        printf( "\nthe maximum value in the column %zu n is: %d", j, max );
    }
}

int main(void) 
{
    size_t m, n;
    
    printf( "enter the size of array (rows) & (column) : " );
    scanf( "%zu %zu", &m, &n );
    int a[m][n];

    
    printf( "enter the number : " );
    for ( size_t i = 0; i < m; i++ )
    {
        for ( size_t j = 0; j < n; j++ )
        {
            scanf( "%d", &a[i][j] );
        }
    }
    
    array2d( m, n, a );

    return 0;
}

它的 output 可能看起来像

enter the size of array (rows) & (column) : 4 4
enter the number : 
1 3 2 1
8 4 3 2
1 2 3 4
9 8 7 6
the maximum value in the column 0 is: 9
the maximum value in the column 1 is: 8
the maximum value in the column 2 is: 7
the maximum value in the column 3 is: 6

如果在 function 的循环中使用指针那么 function 可以看下面的方式

void array2d( size_t m, size_t n, int a[m][n] )
{
    for ( int *p = *a; p != *a + n; ++p )
    {
        int max = *p;

        for ( int ( *q )[m] = a + 1; q != a + m; ++q )
        {
            if ( max < **q ) max = *( *q + ( p - *a ) );
        }

        printf( "\nthe maximum value in the column %zu is: %d", ( size_t )( p - *a ), max );
    }
}

在你的代码中,当你声明一个矩阵 a = rows, b = cols 并且你使用 while 循环来使这个条件:“rows>0 or cols>0”(忽略 0 和负数)

#include<stdio.h>

// Function declaration to input and print two dimensional array
  void input_Matrix(int cols ,int matrix[][cols],int rows);
  void output_Matrix(int cols ,int matrix[][cols],int rows);
//Function to know the maximum of each column
  void Max_Cols(int cols ,int matrix[][cols],int rows);

int main()
{
     int rows,cols;
     do
     {
         printf("Enter the size of rows of this Matrix : ");
         scanf("%d",&rows);
     }while(rows<1);
     do
     {
         printf("Enter the size of cols of this Matrix : ");
         scanf("%d",&cols);
     }while(cols<1||cols!=rows);
     int matrix[rows][cols];

     //Input elements in the matrix
     printf("\nEnter elements of [%d,%d] matrix \n",rows,cols);

     input_Matrix(cols,matrix,rows);
     //Print elements in the matrix

     printf("\nDisplay Elements of [%d,%d] matrix \n",rows,cols);
     output_Matrix(cols,matrix,rows);

     //Print the maximum of each cols
     printf("\n\nMaximum of each column :\n");
     Max_Cols(cols,matrix,rows);

     return 0;
}

/**
 * Function to display elements of two dimensional array (matrix)
 * on console.
 *
 * @matrix  2D array to display as input.
 * @rows    Total rows in 2D matrix.
 * @cols    Total columns in 2D matrix.
 */

 void input_Matrix(int cols ,int (*matrix)[cols],int rows)
 {
      for(int i=0;i<rows;i++)
      {
           for(int j=0;j<cols;j++)
           {
                // (*(matrix + i) + j is equivalent to &matrix[i][j]
                scanf("%d", (*(matrix + i) + j));
           }
      }
 }


void output_Matrix(int cols ,int (*matrix)[cols],int rows)
{
      for (int i=0;i<rows;i++)
      { printf("\n");
           for (int j=0;j<cols;j++)
           {
               // *(*(matrix + i) + j) is equivalent to &matrix[i][j]
               printf("%d ", *(*(matrix + i) + j));
           }
      }
}

void Max_Cols(int cols ,int (*matrix)[cols],int rows)
{
      for (int j=0;j<cols;j++)
      {int *max=matrix[0][j];
           for (int i=0;i<rows;i++)
           {
              // *(*(matrix + i) + j) is equivalent to &matrix[i][j]
              if(*(*(matrix + i) + j)>max)
              {
                   max=*(*(matrix + i) + j);
              }
           }
           printf("\nThe Maximum of column %d is : %d\n",j,max);
      }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM