-2

I have written the below code but I am getting these errors and warnings which I am unable to resolve within this code.

In function 'main':
[Warning] passing argument 1 of 'matrix_read' makes integer from pointer without a cast
[Note] expected 'int' but argument is of type 'int (*)[(sizetype)(no_of_columns)]'
In function 'matrix_read':
[Error] subscripted value is neither array nor pointer nor vector

#include <stdio.h>

    int no_of_rows, no_of_columns;
    int matrix_read(int read_input);
    
int main() {
    
    int matrixA[no_of_rows][no_of_columns];
    
    printf("Enter the number of rows:");
    scanf("%d", &no_of_rows);
    
    printf("Enter the number of columns:");
    scanf("%d", &no_of_columns);
    
    matrix_read(matrixA);
    
    return 0;
}


//Function to read the value from the users

int matrix_read(int read_input){
    
    int i,j;
    for(i=0; i < no_of_rows; i++ ){
        for(j=0; j < no_of_columns; j++){
            
            printf("Enter the elemnts [%d][%d]: ", i+1, j+1);
            scanf("%d", &read_input[i][j]);
                        
        }
    }
    
    
} ```
  • See [this C reference website](https://en.cppreference.com/w/c), and [this answer](https://stackoverflow.com/a/41410503/841108). Read the documentation of your C compiler, e.g. [GCC](http://gcc.gnu.org/) – Basile Starynkevitch Feb 01 '21 at 13:55
  • 3
    Also note that you are initialising your `matrixA` array ***before*** you know the values of `no_of_rows` and `no_of_columns`. You'll end up with a zero-sized array. – Adrian Mole Feb 01 '21 at 13:56
  • 1
    Several issues, first one: `int matrixA[no_of_rows][no_of_columns];` What are the values of the dimensions here? – Damien Feb 01 '21 at 13:56
  • 2
    `MatrixA` is a 2D array, but `read_input` in `matrix_read(int read_input)` is an `int`. You cannot assign an array to an `int`. – Jabberwocky Feb 01 '21 at 14:02
  • Thanks, @AdrianMole I have resolved the conflict as you mentioned can you help me get through the answer? I am still facing the problem – Jaskirat Singh Feb 01 '21 at 14:06
  • @JabberwockyAs per my understanding and knowledge we can assign the array as int data type – Jaskirat Singh Feb 01 '21 at 14:07

2 Answers2

1
#include <stdio.h>

    int no_of_rows, no_of_columns;
    int matrix_read(int read_input);
    
int main() {
    
    int matrixA[no_of_rows][no_of_columns];
    
    printf("Enter the number of rows:");
    scanf("%d", &no_of_rows);
    
    printf("Enter the number of columns:");
    scanf("%d", &no_of_columns);
    
    matrix_read(matrixA);
    
    return 0;
}


//Function to read the value from the users

int matrix_read(int read_input){
    
    int i,j;
    for(i=0; i < no_of_rows; i++ ){
        for(j=0; j < no_of_columns; j++){
            
             int matrixA[i][j];
            
            printf("Enter the elemnts [%d][%d]: ", i+1, j+1);
            scanf("%d",&matrixA[i][j]);
                        
        }
    }
    
    
}

You forgot to mention your array in the function below the main. Function is trying to reach the array but it can not find it. You must define it in the function so that it can reach it. This code is working fine and the only difference is

//int matrixA[i][j]; 
// . 
cigien
  • 57,834
  • 11
  • 73
  • 112
  • What do you mean with *"This code is working fine"*? ([it's not](https://godbolt.org/z/5nc88Gfc6)) What do you think `int matrixA[i][j];` will accomplish? Do you know what are the values of `no_of_rows` and `no_of_columns` when `matrixA` is declared in `main`? How much memory will be allocated to store its values? – Bob__ Dec 30 '21 at 19:51
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 30 '21 at 19:53
0
int matrix_read(void *read_input, size_t no_of_rows, size_t no_of_columns);

int main(void) 
{
    size_t no_of_rows, no_of_columns;
    
    printf("Enter the number of rows:");
    scanf("%zu", &no_of_rows);
    
    printf("Enter the number of columns:");
    scanf("%zu", &no_of_columns);

    int matrixA[no_of_rows][no_of_columns];
    
    matrix_read(matrixA, no_of_rows, no_of_columns);
    
    return 0;
}


//Function to read the value from the users

int matrix_read(void *read_input, size_t no_of_rows, size_t no_of_columns)
{    
    int (*array)[no_of_rows][no_of_columns] = read_input;
    size_t i,j;
    for(i=0; i < no_of_rows; i++ )
    {
        for(j=0; j < no_of_columns; j++)
        {
            
            printf("Enter the elemnts [%zu][%zu]: ", i+1, j+1);
            scanf("%d", &(*array)[i][j]);
        }
    }
    return 0;
}
0___________
  • 60,014
  • 4
  • 34
  • 74