Reputation: 65
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;
}
Upvotes: 0
Views: 809
Reputation: 33931
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.
Upvotes: 1
Reputation: 217135
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;
};
Upvotes: 0