0

Use of 2d array initialisation is forbidden and I am getting this error -

I am instructed to use main() as it is and i can only edit other two functions .

source.cpp(81): error C4700: uninitialized local variable 'Array' used
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Code:

int main(){
    int **Array;
    Array = getArray2D(10, 10, Array);
    for (int i = 0; i<10; i++)
    for (int j = 0; j<10; j++){
        Array[i][j] = 4;
    }
    cout << endl;

    for (int i = 0; i<10; i++){
        for (int j = 0; j<10; j++){
            cout << Array[i][j];
        }
        cout << endl;
    }
    getchar();

    return 0;
}



int* getArray1D(int n, int *A)
{
    A = new int[n];
    for (int i = 0; i < n; ++i)
        A[i] = 0;
    return A;
}
int** getArray2D(int m, int n, int** A)
{
    // m array of integers
    A = new int*[m];
    for (int i = 0; i < m; ++i)
    {
        // create a 1d array on each element of a A
        A[i] = getArray1D(n, A[i]);

    }
    return A;
}

How can i correct this ?

5
  • 2
    the error is self-explanatory - you Array in the first line isn't initialised. Use only getArray2D(10,10,Array). You may have problems with your pointer/variable type declaration Commented Nov 6, 2014 at 11:34
  • I am instructed to use main() as it is and i can only edit other two functions . Commented Nov 6, 2014 at 11:38
  • Then whoever instructed you doesn't have a good understanding of C++. Commented Nov 6, 2014 at 11:39
  • This is the worst way to create a 2d array. Commented Nov 6, 2014 at 11:44
  • 1
    Its just a question to play with pointers and arrays , optimisation and overhead is tottaly ignored . Commented Nov 6, 2014 at 11:47

1 Answer 1

3

The problem is that you don't initialize the Array to NULL and then you use it.

So change this:

int **Array;
Array = getArray2D(10, 10, Array);

to this:

int **Array = NULL;
Array = getArray2D(10, 10, Array);

What you get is actually a warning, not an error, that looks like this:

warning: ‘Array’ is used uninitialized in this function [-Wuninitialized]

Don't forget to de allocate your memory later!

Sign up to request clarification or add additional context in comments.

8 Comments

This will remove the error message, but it isn't the correct fix. That parameter should not exist at all as the function doesn't use it.
The function uses the array @interjay. It could not use it and do it like this gsamaras.wordpress.com/code/2d-dynamic-array-c, but in the OP's code it is used inside the function.
No, it isn't used inside the function. The function overwrites the value that was passed in without using the original value.
I am instructed to use main() as it is .
Yes I agree @interjay. Jimmy yes I saw your comment under the question. So, with the suggest from my answer you overpass the problem you had?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.