Sirmium123
Sirmium123

Reputation: 1

Passing multidimensional arrays to function_111

do_something(int array[][])
{

}

int main()
{
    int array_length;
    cin>> array_length;
    int array[array_length][array_length];
    for()
    {
        "putting elements of array"
    }
}

I have seen people putting some const int so they can pass array to the function. Question is how do I pass a multidimensional array to the function if I don't know its size until it is entered.

Upvotes: 0

Views: 82

Answers (1)

Qwertycrackers
Qwertycrackers

Reputation: 666

int array[array_length][array_length];

This line will not compile. Unlike some other languages (Java, possibly), allocating an array of dynamic size is different than one of constant size. The [] notation will create a constant-size array, which will not work your attempt to pass it array_length. To allocate this array dynamically, use

int **array = new int*[array_length];

The you'll need to iterate through array and allocate each sub-array to the correct size with

array[i] = new int[array_length];

After this, you'll need to reference array_length when iterating through array, as it is your only indication as to the size of the array.

Upvotes: 1

Related Questions