1

I was trying to make a program in which the user decides the dimensions of a 2-D array. I'm getting an error on the function definition while compiling. Why is this wrong and what would be the correct way to do it?

I'm using the Dev-C++ 5.7.1 compiler (if that's relevant).

#include<iostream>

using namespace std;

int R=0,C=0;                 

void func(int);

int main() {
    cin>>R>>C;
    int array[C][R];
                      // DO STUFF HERE
    func(array);
                      // DO SOME MORE STUFF
    return 0;
}

void func(int arr[][R]) {  
                     // DO STUFF HERE
}

3 Answers 3

2

ISO-C++ forbids VLAs. To dynamically allocate an array you'll either need to do some raw pointer tricks or use a vector of vectors.

vector of vectors approach:

std::cin >> R >> C;
std::vector<std::vector<int>> array(R, std::vector<int>(C));

The signature for func then becomes (const correctness may be different)

void func(const std::vector<std::vector<int>>& v);

The above is the easier, more maintainable, safer, shorter solution.


With pointers and pointers to pointers you can do it but it becomes more complicated, and you need to delete everything that you new

int R, C;
std::cin >> R >> C;
int **array = new int*[R]; // allocates space for R row pointers
for (int i = 0; i < R; ++i) {
    array[i] = new int[C]; // for each row, allocate C columns
}

func(R, C, array);

//then delete everything
for (int i = 0; i < R; ++i) {
    delete [] array[i]; // delete all of the ints themselves
}
delete [] array; // delete the row pointers.

with the signature for func being

void func(int r, int c, int **arr);

again, vector of vectors will be a lot easier on you.

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

Comments

1

An array can be located in two different memory regions - on the stack, or on the heap.
Array like you specified, is located on the stack.

int array[SIZE];

when an array is located on the stack, the compiler needs to know in advance what is its size, therefor SIZE must be a constant expression (known at compile time), and sometimes a defined value (set using #define SIZE 10).
if you want to create an array of unknown size (will be determined in runtime), you need to create the array on the heap, like this:

int **array = new int*[C];
for(int i = 0;i<C; i++)
  array[i] = new int[R];

later on, you must remember to delete everything you dynamically allocated (everything you used new on)

for(int i = 0;i<C; i++)
  delete[] array[i];
delete[] array;

note the use of delete[], because we are deleting an array (array is an array of arrays of ints, array[i] is an array of ints)

2 Comments

prefer enum { SIZE = 10 }; or in C++11 constexpr std::size_t SIZE = 10; because it's actually part of C++ (as opposed to the preprocessor substitution) and they have scope.
it's not that SIZE must be const, it's that it must be a constant expression. You can have a const variable where the value is not known at compile time.
0

I would recommend passing in a pointer to the array, and two variables for R and C. Then it's up to you to make sure you use pointer math correctly to stay within the bounds of the array. Otherwise set this up as a template, but you'll still probably need to know the sizes of R & C.

2 Comments

How exactly do you pass a pointer to a 2-D array as a parameter? And how can you convert it back to a 2-D array within the function? (I'm relatively new to C++) Thanks!
I think Ryan covered it pretty well in the second part of his answer. Inside the function just use array[x][y]. This is the C answer on how to do it. The first part of Ryan's answer is the more proper C++ way to do it.

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.