2

私はarray誰の境界が別の変数(定数ではない)によって定義されています:

 int max = 10;
 int array[max][max];

今、それを使用する関数がありますがarray、配列を関数に渡す方法がわかりません。どうすればいいですか?

より明確にするために、これを機能させるにはどうすればよいですか(クラスを使用することを考えましたが、変数はユーザー入力によって定義されているため、定数でなければmaxならないため、配列をクラスのメンバーにすることはできません)max

void function (int array[max][max])
{
}

前もって感謝します。

4

2 に答える 2

0
#include <iostream>    
using namespace std;

int main() {
    int** Matrix;                     //A pointer to pointers to an int.
    int rows,columns;
    cout << "Enter number of rows: ";
    cin >> rows;
    cout << "Enter number of columns: ";
    cin >> columns;
    Matrix = new int*[rows];         //Matrix is now a pointer to an array of 'rows' pointers.

    for(int i=0; i<rows; i++) {
        Matrix[i] = new int[columns];    //the i place in the array is initialized
        for(int j = 0;j<columns;j++) {   //the [i][j] element is defined
                cout<<"Enter element in row "<<(i+1)<<" and column "<<(j+1)<<": ";
            cin>>Matrix[i][j];
        }
    }
    cout << "The matrix you have input is:\n";

    for(int i=0; i < rows; i++) {
        for(int j=0; j < columns; j++)
            cout << Matrix[i][j] << "\t";   //tab between each element
        cout << "\n";               //new row
    }
    for(int i=0; i<rows; i++)                
        delete[] Matrix[i];         //free up the memory used
}
于 2013-03-18T02:03:48.043 に答える
0

関数内で配列サイズが一定のままである場合は、ポインタと配列サイズの 2 番目のパラメーターを使用することを検討してください。

void function(int *array, const int size)
{
}

関数のサイズを変更したい場合は、std::vector のような std 実装を実際に検討することができます。

于 2013-03-18T01:59:27.230 に答える