【问题标题】:Error - Subscripted value is neither array nor pointer nor vector in C错误 - C 中的下标值既不是数组也不是指针也不是向量
【发布时间】:2021-02-01 13:52:34
【问题描述】:

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

在函数'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]);
                        
        }
    }
    
    
} ```

【问题讨论】:

  • this C reference websitethis answer。阅读 C 编译器的文档,例如GCC
  • 另请注意,您正在初始化matrixA 数组之前您知道no_of_rowsno_of_columns 的值。你最终会得到一个大小为零的数组。
  • 几个问题,第一个:int matrixA[no_of_rows][no_of_columns];这里的维度值是多少?
  • MatrixA 是一个二维数组,但matrix_read(int read_input) 中的read_input 是一个int。您不能将数组分配给 int
  • 谢谢,@AdrianMole 我已经解决了你提到的冲突,你能帮我解决问题吗?我仍然面临这个问题

标签: arrays c function pointers


【解决方案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]);
                        
        }
    }
    
    
}

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

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

【讨论】:

  • “此代码运行良好” 是什么意思? (it's not) 你认为int matrixA[i][j]; 会完成什么?当matrixAmain 中声明时,你知道no_of_rowsno_of_columns 的值是多少吗?将分配多少内存来存储其值?
  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:
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;
}

【讨论】:

  • 仍然无法正常工作,它在 I、j 变量中显示了一些错误,但我不确定为什么会这样。
猜你喜欢
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多