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

并期望此输出:

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]

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

所以在这个函数调用中

array2d(x,a,b);

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

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

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

此外,您在函数中使用了另一个可变长度数组 max,其大小由未初始化的变量 j.

设置
    int i,j;
    int max[j];

无需声明此数组即可输出最大值。

该函数也有 return 类型 int 但 return 什么都没有。

所以 any 中的函数是不正确的,没有意义。

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

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;
}

它的输出可能看起来像

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

如果在函数的循环中使用指针那么函数可以看成下面的样子

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);
      }
}