0

So what I am trying to achieve is to return a pointer to a 2D array from the function so it could accessed in main(). I know there are some C++ libraries that does it for you like std::vector but I am trying to avoid dynamic memory allocation since I am working on embedded board (STM32) so I will stick to just normal pointers and arrays. (ALSO for some reason I can't use std::array in KEIL uVision, which is also why I am forced to work with pointers/arrays)

In addition, I understand that returning a pointer to a local array int arr[2][2] defined inside the function is not a good idea since it will no longer be valid after the function returns, which is why I creating test_array, declaring it inside a class and defining it in a function (acting as a global variable) so I assume this shouldn't be a problem. What do you guys think? However, doing it this way gives an error "Excess elements in scalar initializer"

#include <iostream>
#include "file.hpp"

int main() {

  myClass class_object;

  class_object.value = class_object.foo();


}

//file.hpp

#include <stdio.h>

class myClass{

  int array[2][2];
  int (*foo())[2];
  int (*value)[2];

  int test_array[2][2];   //declaring here! 

}; 

//file.cpp 

#include "file.hpp" 

int (*myClass::foo())[2]{

    test_array[2][2]={ {10,20}, {30, 40} }; //defining here - ERROR!!

    int arr[2][2]= {
        {1, 10},
        {20, 30}
    };


return arr;


}
18
  • 2
    int (*myClass::foo())[2]{ is this some new way of doing function declarations that I haven't caught up with yet? It looks like gobbledegook! Also you are still returning the local which you said you knew not to do. Finally, how about telling us where the error occurred? Commented Jun 5, 2017 at 23:10
  • 3
    Why are people so obsessed with C arrays? :( Commented Jun 5, 2017 at 23:11
  • 2
    @Baum Because incompetent profs are teaching them these are important, Industry could save a load of money by just getting rid of these people. Commented Jun 5, 2017 at 23:12
  • 1
    test_array[2][2] is an invalid access past the end of the member array, not a name for the array. And you can't assign an array. If you used std::array<std::array<int,2>,2>, you might have fewer problems. Commented Jun 5, 2017 at 23:12
  • 1
    Please please please use std::array Commented Jun 5, 2017 at 23:12

2 Answers 2

1

The immediate problem:

test_array[2][2]={ {10,20}, {30, 40} }; //defining here - ERROR!!

is not defining. test_array was defined up in myClass. This is attempting to assign to a single element of test_array, specifically [2][2] which does not exist. What particularly offends the compiler is not the out of bounds access, but that ={ {10,20}, {30, 40} }; is trying to stuff an array into a single array element. The compiler is expecting a single number, so four numbers is definitely in excess.

Unfortunately I don't know of a good way to do what you want to do. You can initialize an array with an initializer list, but you can't assign from one.

So

class myClass{
public:

    myClass();
    void foo();

    int test_array[2][2];   //declaring here!
};

// you can do this:
myClass::myClass(): test_array{ {10,20}, {30, 40} }
{

}

void myClass::foo()
{
    // but you can't do this:
    test_array = { {10,20}, {30, 40} };
}

Depending on what you do with test_array, initializing in the constructor may work for you. If you have to reset the array on every call to foo, perhaps an Automatic variable is a better fit for you

void myClass::foo()
{
    int temp_array[2][2] = { {10,20}, {30, 40} };

    // use temp_array

    // maybe copy temp_array to test_array with good ol' memcpy here if you 
    // need to carry the state for some reason.
}

To silence the elephant in the room and gain access to std::array, give this a try. Note: I've never done this. It could be an utter freaking disaster for all I know, so take it with a grain of salt.

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

Comments

0

If you really want to work with C-Array, use typedef to have normal syntax:

class myClass{
    public:
    using array2 = int[2][2];

    myClass() {
        test_array[0][0] = 0;
        test_array[0][1] = 1;
        test_array[1][0] = 2;
        test_array[1][1] = 3;
    }

    const array2& getArray() const { return test_array; }
    array2& getArray() { return test_array; }

private:
    array2 test_array;
}; 

2 Comments

I don't know this seems a bit perplexed. Plus I don't want to initialize an array inside a class but inside a function as I am implementing some mathematical functions inside it
@Doej if this is embedded platform with tight constraints, it's questionable why don't you remove dynamic memory completely, why don't you have reserved global memory for all possible results (let's say you know the app will produce at most 100 of 2x2 int arrays, so just have the 1600 bytes of memory for results fixed at compile time as a global, and give the function target pointer where it should do its calculation, instead of returning something as impractical as pointer to C array (which usually quickly decays to ordinary int*, losing any advantage of starting as array type).

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.