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);
   

}
4
  • 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. Commented Apr 8, 2022 at 6: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. Commented Apr 8, 2022 at 6:08
  • If you realy need an array, you need dynamic allocation cplusplus.com/doc/tutorial/dynamic. Side note, may be you should try to store what your function return ? Commented Apr 8, 2022 at 6:09
  • 4
    Besides the problem with variable-length arrays, 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. Lastly, please invest in some good C++ books. Commented Apr 8, 2022 at 6:13

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

Comments

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;
}

4 Comments

Why should I not #include <bits/stdc++.h>? Why is "using namespace std;" considered bad practice? And please try to avoid promoting cargo cult programming (which code-only answers tend to do). Also please read about how to create good answers.
Ohh, I just modified his code a little bit. I edited it now.
This code leaks, memory allocated by new must be explicitly freed by delete or delete[] (unless wrapped into smart pointers of course).
Ah yes, you're right!

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.