【问题标题】:Facing this Error: tempCodeRunnerFile.c:3:38: note: expected 'int *' but argument is of type 'int' void printArray(int row,int col,int *marks){面对此错误:tempCodeRunnerFile.c:3:38:注意:预期为“int *”,但参数为“int”类型 void printArray(int row,int col,int *marks){
【发布时间】:2021-11-06 08:51:13
【问题描述】:

我正在尝试打印具有类似功能的二维数组

{ {i,j}{i,j}        
  {i,j}{i,j}  
  {i,j}{i,j} }

通过在二维数组中获取主函数中的值... 请帮助我是编程初学者,学习我的第一门编程语言...

#include <stdio.h>
void printArray(int row,int col,int *marks){
    printf("{ ");
    int j;
     for(int i=0; i<row; i++){
         printf("{");
        for(int j=0; j<col; j++){
            printf("%d", *(marks));
            if(j==0){
            printf(", ");
            }
        }
        printf("}");
        if(i==2,j==1);
     }
     printf("}");
}
int main()
{
    int marks[3][2];
    int row = 3,col = 2;
    int i,j;
     for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            printf("Enter the value [%d][%d]: ",i,j);
            scanf("%d", &marks[i][j]);
        }
    }
    printArray(row, col, marks[i][j]);
    return 0;
}

【问题讨论】:

  • 写 printArray(row, col, tags);
  • 非常感谢...它对我有用????????????...
  • 如果问题得到解决,然后选择最佳答案关闭问题。

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


【解决方案1】:

问题是您调用的函数传递了一个int 类型的表达式,其中使用了未初始化的变量ij,而不是传递数组本身。

int i,j;
//...
printArray(row, col, marks[i][j]);

像这样改变函数调用

printArray(row, col, marks);

以及如下演示程序中所示的函数声明和定义

#include <stdio.h>

void printArray( int row, int col, int marks[][col]){
    printf("{\n");
     for(int i=0; i<row; i++){
         printf("\t{");
        for(int j=0; j<col; j++){
            printf("%d", marks[i][j]);
            if(j != col - 1){
            printf(", ");
            }
        }
        printf("\t}\n");
     }
     printf("}\n");
}

int main(void) 
{
    enum { ROW = 3, COL = 2 };
    int marks[ROW][COL] =
    {
        { 1, 2 },
        { 3, 4 },
        { 5, 6 } 
    };
    
    printArray( ROW, COL, marks );
   
    return 0;
}

程序输出是

{
    {1, 2   }
    {3, 4   }
    {5, 6   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-12
    • 2010-09-24
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2020-02-05
    相关资源
    最近更新 更多