1

I am relatively new to C++ programming (about half a year of C programming learning and 2 months of C++), so my question might seem (or be) stupid, but I've been searching the internet for awhile and also trying to figure out whats going on.

I wrote a code that makes 2d (and later another to 3d) dynamic array which is working fine, but when I put it in to separate function there is a problem when I try to put the data (using cin) to this array.

My code to make 2d array:

double tab** = new double*[x];
for(int i=0; i<x; i++){
    tab[i] = new double[y];
}

And then to put the data into the array:

double tmc;
for(int i=0; i<x; i++){
    for(int j=0; j<y; j++){
        cout<<"Type the value of : "<<i+1<<", "<<j+1<<endl;
        my_fun_input(&tmc);
        tab2d[i][j]=tmc;
    }
}

my_fun_input is my function that looks like:

void my_fun_input(double* number){
cin>>*number;
while(cin.good()==false){
    cout<<"Error! Bad value."<<endl;
    cin.clear();
    cin.ignore();
    cin>>*number;
}}

And the problem is that when I use it directly it' s working, but when I move the first piece of code that is creating the 2d array to separate function like this:

void table(double** tab, int x, int y){
tab = new double*[x];
for(int i=0; i<x; i++){
    tab[i] = new double[y];
}}

I am using this function like this:

 double** tab2d=NULL;
 int x, y; //size obtained from user
 table(tab2d, x, y);

After using this function the code to put data into works for first element and then the program is crushing. What can cause this? Thanks for help in advance.

PS. Sorry for my poor english.

1
  • Even when you solve this problem you have, you are still left with a bigger problem, and that is that you are still trying to write C code, and just compiling it now with C++ compiler. In C++, one would use std::vector to create a 2D array, instead of fiddling with raw pointers (by that I mean double** tab). If you really want to learn C++, try learning to do thing the C++ way. Here is a an article to get you started with using 2D vectors: techiedelight.com/vector-of-vector-cpp Commented Apr 15, 2020 at 11:30

1 Answer 1

1

The function table is not actually modifying the value of the first parameter. The value stored in tab (the new array) is only local to the function.

To make it actually modify the input value, its signature would need to be changed to:

void table(double**& tab, int x, int y){
  tab = new double*[x];
  for(int i=0; i<x; i++){
    tab[i] = new double[y];
}}

Or (to avoid a non-const reference):

void table(double*** tab, int x, int y){
*tab = new double*[x];
for(int i=0; i<x; i++){
    (*tab)[i] = new double[y];
}}
Sign up to request clarification or add additional context in comments.

Comments

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.