arrays - 错误 - 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]);
}
}
} ```
解决方案
#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];
// .
推荐阅读
- php - 我正在尝试使用 symfony 中的 getEntityManager 从存储过程中返回插入的行,但收到的响应为 false
- ajax - 当网页加载重定向时,我看不到在@ViewScoped bean 中的@PostConstruct 方法中生成的菜单
- fragment - 单击列表时想要以 pdf 格式打开文档时出错
- javascript - Bootstrap 4下拉活动类不起作用
- javascript - ExtJS 6.x JSON 数据复杂成模型
- here-api - HERE Maps for Javascript API:计算最接近当前地理位置的路由操作
- node.js - 在猫鼬中格式化日期
- angular - Angular 路由器正在从 iframe 捕获 url
- php - 如何设置 dockerfle 运行 nohup php test.php >myscript.log 2>&1 &
- java - Manifest.xml 中的 Kotlin 内部类广播接收器