0

I'm having a beginners doubt.

How do I pass an user inputted 2d array/vector to a function?

Since, user will be inputting the number of rows and columns, we will ask for the input.

int n, m;
int main(){
    cin >> n >> m;
    return 0;
}

Once inputted we will ask for the values of each cell.

int n, m;
int main(){
    cin >> n >> m;
      
    char ary[n][m];
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
             cin >> ary[i][j];
        }
    }
    return 0;
}

Once we inputted the whole matrix, we would like to print it through a function.

int n, m;

void fun(char ary[n][m]){
     for(int i=0; i<n; i++){
         for(int j=0; j<m; j++){
             cout << ary[i][j] << " ";
         }cout << endl;
     }
}

int main(){
    cin >> n >> m;
         
    char ary[n][m];
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
             cin >> ary[i][j];
        }
    }
         
    fun(ary);
    return 0;
}

Why doesn't this code work? I thought maybe I can use vectors, but I am quite clueless about it as well. Please help me out.

Thank you.

Here's the log after running above code: 2D array passed as param: error:

7
  • 2
    char ary[n][m]; is not standard C++. Array sizes must be compile time constants. You should take a look at std::vector Commented Aug 9, 2022 at 11:37
  • If you want to pass a matrix, then pass a matrix. in other words, put the array in a class/struct and pass that around. Easier syntax and better semantics. For arrays you can also consider to use std::array or std::vector they behave much more like classes, can be passed around as (const) reference and returned from functions (much harder to do right using "C" style arrays) Commented Aug 9, 2022 at 11:38
  • @463035818_is_not_a_number what does array sizes must be compile time constants mean? Can you help me out with how to do it with vector? Commented Aug 9, 2022 at 11:40
  • 1
    Whichever C++ textbook or web site showed you this kind of an example, of declaring an array: throw away that book, or don't visit that web site again. You are not being taught proper C++. See a good C++ textbook for a complete discussion of how to use std::vector instead of arrays, like this. It is not realistic to expect to be able to learn C++ by asking one question at a time. The only way to learn C++ is with a good textbook. Commented Aug 9, 2022 at 11:40
  • In C, with VLA, it would be void fun(int n, int m, char ary[n][m]). For C++, you might use std::vector<std::vector<int>> instead. Commented Aug 9, 2022 at 11:42

1 Answer 1

0

The std::vector has a constructor, where you can specify the size. Please see here, constructor number 3.

With that, you can write

std::vector<std::vector<int>> vec( n , std::vector<int> (m, 0)); 

Then you have a 2d vector.

You will then pass this as reference to you function.

void someFunction(std::vector<std::vector<int>>& vecPara) {
    // Do something with vecPara[i][j]
}

You were using so called VLAs (Variable length arrays). These are not part of the C++ language.

If you want to define a C-Style array in C++, then the size-value must be a constant compile time value, like for exymple "3". It cannot be the value of a variable, because that could change at runtime.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.