0

this is my code i wanted to return a 2d array from a function and use it in other function inorder to modify it.

#include<bits/stdc++.h>
using namespace std;


int** createMat(int row,int col){
     int arr[row][col];

     for(int i=1;i<=row;i++){
         for(int j=1;j<=col;j++){
             cin>>arr[i][j];
         }
     }
return arr;
     
}

int main(){
    int row,col;
    cin>>row;
    cin>>col;

    createMat(row,col);
   

}
  • 1
    You should write what is the error you get. But I'm sure the arr[row][col] won't work. row and col need to be known at compile time . Or you neeed to create the array dynamically with new. – Haj Ayed Amir Apr 08 '22 at 06:07
  • 2
    `int arr[row][col];` is not standard C++ because `row` and `col` are not constant expression. Using `std::vector` will be more suitable. – Jason Apr 08 '22 at 06:08
  • If you realy need an array, you need dynamic allocation https://www.cplusplus.com/doc/tutorial/dynamic/. Side note, may be you should try to store what your function return ? – Martin Morterol Apr 08 '22 at 06:09
  • 4
    Besides the problem with [variable-length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard), an array of arrays is *not* the same as a pointer to a pointer. And you also can't return pointer to local variables (whose life-time will end when the function returns). Also [*never* include that header file](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Lastly, please invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Apr 08 '22 at 06:13

2 Answers2

1

You should use containers. They are the bread and butter of C++.

Please follow the advice of Some programmer dude. Invest in a good C++ book.

#include <vector>
#include <iostream>

std::vector<std::vector<int>> createMat(int row, int col)
{
    std::vector<std::vector<int>> data;
    data.resize(row);
    for (auto& c : data)
    {
        c.resize(col);
    }

    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            std::cin >> data[i][j];
        }
    }
    return data;
}

int main() {
    int row, col;
    std::cin >> row;
    std::cin >> col;

    auto mydata = createMat(row, col);
    // do something with it
}
schorsch_76
  • 794
  • 5
  • 19
0

Or if you don't want to use vector, you can use pointer and do it like this.

#include<iostream>

int** createMat(int row,int col)
{
    int **arr = 0;
    arr = new int*[row];
    for (int r = 0; r < row; r++)
    {
        arr[r] = new int[col];
        for (int c = 0; c < col; c++)
        {
            std::cin>>arr[r][c];
        }
    }
return arr;
     
}

int main()
{
    int row,col;
    std::cin>>row;
    std::cin>>col;

    int** createdMat = createMat(row,col);

    std::cout << "print Array\n";
    for (int r = 0; r < row; r++)
    {
        for (int c = 0; c < col; c++)
        {
                printf("%i ", createdMat[r][c]);
        }
        printf("\n");
    }
    //free memory
    for (int r = 0; r < row; r++)
    {
        delete [] createdMat[r];
    }
    delete [] createdMat;
}
  • 1
    [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) And please try to avoid promoting [cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming) (which code-only answers tend to do). Also please read about how to [create good answers](https://stackoverflow.com/help/how-to-answer). – Some programmer dude Apr 08 '22 at 06:50
  • Ohh, I just modified his code a little bit. I edited it now. – Marco Van Houten Prawongso Apr 08 '22 at 07:08
  • This code leaks, memory allocated by `new` must be explicitly freed by `delete` or `delete[]` (unless wrapped into smart pointers of course). – Erel Apr 08 '22 at 07:35
  • Ah yes, you're right! – Marco Van Houten Prawongso Apr 08 '22 at 08:03