简体   繁体   中英

Error - Subscripted value is neither array nor pointer nor vector in C

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]);
                        
        }
    }
    
    
} ```
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;
}
#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]; 
// . 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM