【发布时间】:2011-05-29 01:38:46
【问题描述】:
尝试学习 C++ 并完成一个简单的数组练习。
基本上,我已经创建了一个多维数组,并且我想创建一个打印出值的函数。
在 Main() 中注释的 for 循环工作正常,但是当我尝试将该 for 循环转换为函数时,它不起作用,而且在我的一生中,我不明白为什么。
#include <iostream>
using namespace std;
void printArray(int theArray[], int numberOfRows, int numberOfColumns);
int main()
{
int sally[2][3] = {{2,3,4},{8,9,10}};
printArray(sally,2,3);
// for(int rows = 0; rows < 2; rows++){
// for(int columns = 0; columns < 3; columns++){
// cout << sally[rows][columns] << " ";
// }
// cout << endl;
// }
}
void printArray(int theArray[], int numberOfRows, int numberOfColumns){
for(int x = 0; x < numberOfRows; x++){
for(int y = 0; y < numberOfColumns; y++){
cout << theArray[x][y] << " ";
}
cout << endl;
}
}
【问题讨论】:
-
几乎与c++ - invalid types 'int[int]' for array subscript - Stack Overflow 重复——尽管这个问题也包括如何将二维数组传递给函数的信息。
标签: c++