繁体   English   中英

错误 - C 中的下标值既不是数组也不是指针也不是向量

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

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

在 function '主要':
[警告] 传递 'matrix_read' 的参数 1 使 integer 从没有强制转换的指针
[注意] 预期为“int”,但参数的类型为“int (*)[(sizetype)(no_of_columns)]”
在 function '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]);
                        
        }
    }
    
    
} ```
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]);
                        
        }
    }
    
    
}

您忘记在主下方的 function 中提及您的阵列。 Function 正在尝试访问阵列,但找不到它。 您必须在 function 中定义它,以便它可以访问它。 这段代码工作正常,唯一的区别是

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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