0

I have an assignment to get command line arguments from a user - the user inputs two ints to define the rows and columns of a dynamic array. The array is then filled with random numbers. I can't seem to create the array, as I keep getting invalid conversion errors no matter what I try. Can anyone clear up what I'm misunderstanding here?

I've reproduced the relevant code and error message per line below:

int main(int argc, char* argv[]) {

/* Cut out code irrelevant to question that gets the arguments 
   to define columns and rows. This part works fine. */

    int * array = new int[rows];

    int i, j;
    for (i = 0; i < rows; i++) {
            array[i] = new int[columns];  // error: invalid conversion from ‘int*’ to ‘int’

    }

    int * pointer = array;

    array = randomArray(pointer, rows, columns);

    for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                    cout << array[i][j] + " " << endl;  // error: invalid types ‘int[int]’ for array subscript

            }
    }

    return 0;

}


int* randomArray(int array[], int rows, int columns) {

    srand (time(0));

    for (i = 0; i < rows; i++) {
            for (j = 0; j < columns; j++) {
                    int random = rand() % 100;
                    array[i][j] = random; // error: invalid types ‘int[int]’ for array subscript
            }
    }
    return array;

    for (i = 0; i < rows; i++) {
            delete[] array[rows];
    }
    delete[] array; // error: type ‘int’ argument given to ‘delete’, expected pointer
}

I've managed to clean up most of the errors so far but I can't seem to fix these last ones. I know I'm missing something important about 2d arrays. Any tips?

1 Answer 1

3

You want array to be an array of pointers to int arrays so

int * array = new int[rows];

should be

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

and

int* randomArray(int array[], int rows, int columns)

would become

int** randomArray(int *array[], int rows, int columns)

or, as pointed out by WhozCraig,

void randomArray(int *array[], int rows, int columns)

since the return value isn't required.

Alternatively, if it doesn't violate constraints of your assignment, you could use std::vector instead

std::vector<std::vector<int>> array;
Sign up to request clarification or add additional context in comments.

4 Comments

+1 Note: once implemented this way, the return value of randomArray is pointless. If randomArray() actually allocated the array and only took the two dimensions as parameters, it would obviously be required.
@WhozCraig Thanks, I hadn't checked that the implementation of the function required the current signature. I've noted this in my answer now.
Thanks for the help. I'm still having a few problems getting it to compile... I need to be able to pass this array to the randomArray function but I can't seem to get it to work. I created the int * pointer as a dummy to pass to the function, but once I implemented your suggestions, now I get this error when initializing it: cannot convert ‘int*’ to ‘int**’ for argument ‘1’ to ‘void randomArray(int**, int, int)’ and this error when using it as a parameter: error: cannot convert ‘int*’ to ‘int**’ for argument ‘1’ to ‘void randomArray(int**, int, int)’
You need to pass the address of a int* variable (using the & operator) to have it treated as int**

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.