【问题标题】:Pointers, function and 2d array in c programc程序中的指针、函数和二维数组
【发布时间】:2020-12-06 09:29:11
【问题描述】:

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

 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

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

【问题讨论】:

  • 查看this answer相关问题
  • 看完还是没看懂@BasileStarynkevitch
  • 那你需要读一本C编程的好书,比如Modern C。读完那本书后,请参阅 this website 并阅读一些 C 草案标准,例如 n1570
  • 另请阅读 C 编译器的文档(例如 GCC...)。编译所有警告和调试信息(所以gcc -Wall -Wextra -g)。然后使用您的调试器(例如GDB...)来了解您的程序的行为。您可以使用GNU emacs 来编辑您的源代码。 StackOverflow 不是一个做我的家庭作业服务
  • githubgitlab上的现有开源程序中汲取灵感

标签: c pointers multidimensional-array implicit-conversion function-declaration


【解决方案1】:

这里有一个工作示例:

查看指针算法。

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

【讨论】:

  • 谢谢,但你能用简单的c代码写吗
【解决方案2】:

你声明了一个二维变长数组

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

不需要声明这个数组来输出最大值。

该函数也有返回类型int,但什么也不返回。

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

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

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

【讨论】:

  • 谢谢,但我正在寻找使用指针
【解决方案3】:

在您的代码中,当您声明矩阵 a = rows, b = cols 并使用 while 循环进行此条件时:“rows>0 或 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);
      }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    相关资源
    最近更新 更多