C++ 传递 2D 数组以运行时出错
C++ Error passing a 2D array to function?
>编辑:ROW 和 COLUMN 是整数值,ROW = 12,COLUMN = 2
int main() {
double list[ROW][COLUMN];
ifstream inFile;
getValidDataFile(inFile);
cout << "Temperature Data for the year " << getYear(inFile) << endl;
getData(inFile, list[][COLUMN], ROW); // Error Line
return 0;
}
错误:"错误:']' 标记之前的预期主表达式"我需要从文件中获取数据并将其存储在 2d 数组中。顺便说一句,这是一个家庭作业
void getData(ifstream& fin, double a[][COLUMN], int ROW) {
int row, col;
double num;
for(row = 0; row < ROW; row++) {
for(col = 0; col < COLUMN; col++) {
fin >> num;
a[row][col] = num;
}
}
}
当你调用getData()时,你应该在不指定维度的情况下传入数组。 在声明列表 [X][Y] 之后,将访问第 X 行 Y 列中的单个元素。
getData(inFile, list, row);
此外,建议只对宏使用大写,而不对函数参数使用:
void getData(ifstream& fin, double a[][COLUMN], int input_row) {
您可以在
声明和定义函数时提及列大小的最大大小,并且像往常一样将数组的基址传递给函数
void print(int p_arr[][10]); //max size of the column -- declaration
int g_row,g_column;//make it as these variables as global;
int main()
{
int l_arr[10][10];//local array
printf("Enter row value and column value");
scanf("%d%d",&g_row,&g_column);
for(int i=0;i<g_row;i++)
{
for(int j=0;j<g_column;j++)
{
scanf("%d",&l_arr[i][j]);
}
}
print(l_arr);//you just pass the array address to the function
return 0;
}
void print(int p_arr[][10])
{
for(int i=0;i<g_row;i++)
{
for(int j=0;j<g_column;j++)
{
printf("%dt",p_arr[i][j]);
}
printf("n");
}
return;
}
将其作为带有行和列信息的双指针传递会更容易。所以你的代码将是
void getData(ifstream *fin, double** a, int row, int col);
函数定义将保持不变
getData(inFile, list, row, col);
是使用它的方式。
相关文章:
- 分段错误当我试图运行程序时出错
- 运行命令 "rosbag play example.bag" 时出错。找不到与插件 rosbag/NoEncryptor 对应的库
- 从反向顺序C++访问矢量时运行时出错
- 合并两个排序链表时运行时出错
- 从主线程中询问运行对象表 (ROT) 时出错
- 在VS2015中访问类成员时运行时错误,但在Linux上未访问时出错
- 在 Qt 中将 x 可执行文件作为按钮事件运行时出错
- 运行时动态加载库时出错"unresolved external symbol"
- 尝试使用浮点参数运行重载函数时出错
- 可执行文件在运行时出错
- 在 Eclipse.It 中运行C++代码时出错,'Your program cannot output..'
- OpenCV C++代码在终端中运行良好,但在从Ubuntu 12.04运行时出错
- C++程序生成成功,但在运行时出错
- boost::spiritqi解析运行时出错
- 为我的生成文件运行时出错.cpp
- 确保程序在运行时出错时在 C++/fortran 中的适当位置崩溃
- C++ 传递 2D 数组以运行时出错
- Mysterios Malloc 在运行时出错
- 运行时出错,在线判断上的退出代码为6
- 如何修改C++运行时出错的字符串