首页 > 解决方案 > 错误 - C 中的下标值既不是数组也不是指针也不是向量

问题描述

我已经编写了以下代码,但我收到了这些错误和警告,我无法在此代码中解决这些错误和警告。

在函数'main'中:
[警告]传递'matrix_read'的参数1从没有强制转换的指针生成整数
[注意]预期'int',但参数的类型是'int(*)[(sizetype)(no_of_columns)]
'函数'matrix_read':
[错误]下标值既不是数组也不是指针也不是向量

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

标签: arrayscfunctionpointers

解决方案


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

您忘记在 main 下方的函数中提及您的数组。函数试图到达数组但找不到它。您必须在函数中定义它,以便它可以访问它。这段代码工作正常,唯一的区别是

//int matrixA[i][j]; 
// . 

推荐阅读