Home > Blockchain >  Facing this Error: tempCodeRunnerFile.c:3:38: note: expected 'int *' but argument is of ty
Facing this Error: tempCodeRunnerFile.c:3:38: note: expected 'int *' but argument is of ty

Time:09-17

I'm trying to print a 2D array with function like

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

by taking values in main function in a 2D array... please help I'm a beginner in programming learning my first programming language ...

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

CodePudding user response:

The problem is that you are calling the function passing to it an expression of the type int where there are used uninitialized variables i and j instead of passing the array itself.

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

Change the function call like

printArray(row, col, marks);

and the function declaration and definition like shown in the demonstrative program below

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

The program output is

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