-2

I want to send a 2D array from the main function to another function, and I want to return a 1D array. I do not know how to return an array from a C++ function. When I do it for a scalar value (not a vector or array), it works fine. But with arrays, I have problems. Here is my code:

#include <iostream>
using namespace std;

float display(float n[3][2]);       // declare my function
int main()                          // main function
{
    float num[3][2] = {             // a dummy 2D array
        {3.3, 4.3},
        {9.3, 5.3},
        {7.3, 1.3}
        };
    float a[3];
    a = display(num);               // send array to display function   // line 13
    for(int i = 0;  i < 3; ++i)
    {
    cout << "reurned array is : " << a[i] << endl;
    }
    return 0;
}

float display(float n[3][2])        // define my function
{
    float b[3];
    cout << "Displaying Values: " << endl;
    for(int i = 0;  i < 3; ++i)
    {
        b[i] = n[i][0];
        for(int j = 0; j < 2; ++j)
        {
          cout << n[i][j] << " ";
        }
    }
    cout << endl;
    for(int i = 0;  i < 3; ++i)
    {
    cout << "actual array is : " << b[i] << endl;
    }
return b;               // line 39
}

And this is the error I am getting:

/main.cpp||In function ‘int main()’
/main.cpp|13|error: incompatible types in assignment of ‘float’ to ‘float [3]
/main.cpp||In function ‘float display(float (*)[2])’
/main.cpp|39|error: cannot convert ‘float*’ to ‘float’ in return
  • 1
    Instead of having "in C++" in the title, why don't you just tag with [tag:c++]? – user202729 Apr 10 '18 at 14:03
  • 1
    [Please format the error messages](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks). – user202729 Apr 10 '18 at 14:04
  • 1
    And don't use C arrays: [what-are-some-of-the-drawbacks-to-using-c-style-strings](https://stackoverflow.com/questions/312570/). – user202729 Apr 10 '18 at 14:05
  • In C++, it's likely better to use `std::array` of `std::array`s or `std::vector` of `std::vector`s. However, for performance reasons, it's typically better to store 2D array data in a 1D array/vector, see, e.g., https://stackoverflow.com/questions/19913596/c-2d-array-to-1d-array for details. – Daniel Langr Apr 10 '18 at 14:10
  • 2
    @DanielLangr `std::array,Y>` is flat (use contiguous memory) anyway. – user202729 Apr 10 '18 at 14:11
  • @user202729 Nice feature, didn't know that. – Daniel Langr Apr 10 '18 at 14:12
  • @user202729 : That's a bad duplicate. In *this* question, the user wants to return a local variable (which is not directly possible), in *that* question, the questioner wanted to return an argument (which is). – Martin Bonner supports Monica Apr 10 '18 at 14:29
  • @MartinBonner I see, (hence the warning), but I (currently) can't find a better one. – user202729 Apr 10 '18 at 14:30
  • @Umair: I have closed this as a duplicate, but I would ignore the top-voted and accepted answer. Instead prefer https://stackoverflow.com/a/4264449/771073 – Martin Bonner supports Monica Apr 10 '18 at 14:35
  • Guys, this is my first ever question in this platform. I know, even i didn't format the error message correctly. Anyway, i will look for other similar questions. Thanks for your comments. – Umair Khan Apr 10 '18 at 14:39
  • If you change into `std::vector b(3);` now you can properly return b. In early days, we used to do naked owning pointer like `float *b = new float[3];` but if one forget to delete means memory leaks. That could turn into RAII like `auto b = std::make_unique(3);`. However, useing containers are most convenient anyway. – sandthorn Apr 10 '18 at 21:28

1 Answers1

-1
One possible solution is:

#include <stdio.h>

typedef float farray[3][2];

float *display(farray n);       // declare my function

int main()                          // main function
{
    farray num = {             // a dummy 2D array
        {3.3, 4.3},
        {9.3, 5.3},
        {7.3, 1.3}
        };
    float *a;
    int i;

    a = display(num);               // send array to display function   // line 13
    printf("returned array is: ");
    for(i = 0;  i < 3; ++i)
    {
        printf("%f ", a[i]);
    }
    printf("\n");
    return 0;
}

float *display(farray n)        // define my function
{
    static float b[3];
    int i;

    printf("Displaying Values: \n");
    for(i = 0;  i < 3; ++i)
    {
        b[i] = n[i][0];
        for(int j = 0; j < 2; ++j)
        {
            printf("%f ", n[i][j]);
        }
    }
    printf("\n");
    printf("actual array is : ");
    for(i = 0;  i < 3; ++i)
    {
        printf("%f ", b[i]);
    }
    printf("\n");
    return b;               // line 39
}