1

I am trying to access the values pointed by pointers in a 2D array of double pointers, passed as a function arguments

float cfun (float **indatav, int rows, int cols)
{
    float* *outdatav = new float*[rows];
    for (int i=0; i < rows; i++){
        outdatav[i] = new float[cols];
    }

    for (int i=0; i < rows; i++){
        for (int j=0; j < cols; j++){
            outdatav[i][j] = *(*(indatav[i])[j]);
            }
    }

    return outdatav;
}

I have tried several variants but none of them works. This variant seems to me the most correct but doesn't work and I don't understand why. Any hint or explanation would be highly appreciated.

I know that having outdatav[i][j] = indatav[i][j]; with a return type of float** could work but this is not what I am looking for, because in this case the function would return the memory adress of the double pointers, while I am looking for the values of the variables pointed by those pointers.

A reproducible example would be:

#include <iostream> 
#include <vector>
using namespace std;

//float cfun (float **indatav, int rows, int cols)
float** cfun (float **indatav, int rows, int cols)
{
    float** outdatav = new float*[rows];
    for (int i=0; i < rows; i++){
        outdatav[i] = new float[cols];
    }

    for (int i=0; i < rows; i++){
        for (int j=0; j < cols; j++){
            outdatav[i][j] = *(*(indatav[i])[j]);
        }
    }
    return outdatav;
}

int main(){

//  float indatav[2][2] = {{1.,2.};{3;4}} // WRONG!
    float indatav[2][2] = {{1.,2.},{3,4}};// RIGHT!
    int rows = 2, cols = 2;

//  float y[2][2] = cfun(**indatav, rows, cols); // Try compiling this!
    float **y = cfun(indatav, rows, cols); // THIS DOES WHAT YOU ARE AKSING!
    return 0;
}

Where ywould be have the values {{1.,2.},{3,4}}. // Note changes

2
  • Please try to create a minimal reproducible example to show us, especially how you call the function cfun. And what do you mean by "none of them works"? Do you get build errors? Wrong results when running? Crashes? Something else? Please take some time to refresh how to ask good questions, as well as this question checklist. Commented Sep 29, 2019 at 12:39
  • The second code block you gave won't even compile! Try. Then try with the changes I've made - at least then it will compile. Commented Sep 29, 2019 at 16:54

2 Answers 2

1

After a long discussion I understood I was not passing correctly the arguments in the functions and was messing up with the pointers and data-types. Here is the final answer:

#include <iostream>
#include <vector>
using namespace std;

float** cfun (float **indatav, int rows, int cols) 
{
    float* *outdatav = new float*[rows];
    for (int i=0; i < rows; i++){
        outdatav[i] = new float[cols];
    }

    for (int i=0; i < rows; i++){
        for (int j=0; j < cols; j++){
            outdatav[i][j] = indatav[i][j]; 
            }
    }
    return outdatav; 
}

int main(){

    int rows = 2, cols = 2;
    float list[rows][cols]= {{1.,2.},{3.,4.}};

    float* *indatav = new float*[rows];
    for (int i=0; i < rows; i++){
        indatav[i] = new float[cols];
    }

    for (int i=0; i < rows; i++){
        for (int j=0; j < cols; j++){
          indatav[i][j]=list[i][j];
        }
    }

    float **y = cfun(indatav, rows, cols);

    for(int i=0; i<rows; i++){
      for(int j=0; j<rows; j++){
        cout << y[i][j] <<endl;
      }
    }

    return 0;
}

Obviously I am open to any more elegant solution for the main function.

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

1 Comment

Glad it all worked out in the end! Your solution is actually a good one!
0

You seem very comfortable when you correctly dereference your outdatav 2D array, so what makes you think the indatav 2D array should be treated any differently? Try the following two-line correction:

float** cfun (float **indatav, int rows, int cols) // Now return 2D pointer!
{
    float* *outdatav = new float*[rows];
    for (int i=0; i < rows; i++){
        outdatav[i] = new float[cols];
    }

    for (int i=0; i < rows; i++){
        for (int j=0; j < cols; j++){
        //  outdatav[i][j] = *(*(indatav[i])[j]);
            outdatav[i][j] = indatav[i][j]; // Why don't you think this will work?
            }
    }

    return outdatav; // Note change in function declaration!
}

After all, both outdatav and indatav have exactly the same declarations - both are float**.

In your edited question, you now have a call: y = cfun(**indatav, rows, cols); and you add

Where y would be have the values {{1.,2.},{3;4}}

This is exactly how I have interpreted what you want. You would need to declare your variable, "y," as: float** y and the actual value for the data at row 0 and column 1 would be given by the expression y[0][1]. However, your call to cfun wrong and should be: y = cfun(indatav, rows, cols); for the example you have given.

15 Comments

There is one additional problem: return outdatav; won't compile, because outdatav is declared as float** outdatav, but function is declared as returning float: float cfun.
in my opinion, taking into account, the functionality of the function, asker probably wanted to just return float**. But, that's only my opinion, and a guess. Since it's hard to know the intention without the minimal reproducible example.
@AlgirdasPreidžius Indeed, otherwise the allocated pointers are lost to the world!
@Adrian First, thank you for your response. What I meant by "some variants" is that I have precisely tried the answer you are giving, but this just returns the values of the pointers **indatav. I don't want the position of the pointers in the memory of the computer, I want the values which are pointed by the pointers in **indatav, and I would like my function to return those values. This is also why I left as return type of the function float and not **float. I don't know if this is clear enough ? Please tell me if it still isn't. I will also adapt my question.
@AlgirdasPreidžius No I really want to return the values pointed by the pointers of the 2 dimensional array. So I really want to return floatand not **float.
|

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.