I'm doing shenanigans with pointers for an Arduino hobby project, and I've come across a situation I can't resolve.
Basically, I have a global (because Arduino) array of pointers to Thing
. I don't want to initialize it during the declaration, but I want to take advantage of the nice array initialization syntax.
Please note that this is an Arduino project and so uses an unusual mix of C and C++, because Arduino. From some basic research, looks like that if it'll work in C, or C++ without libraries, it'll work for this.
Note: I'm using NULL as a way to detect the end of the array as I loop over it, elsewhere in the code.
Something like so:
struct Thing {
int i;
};
Thing * MakeThing() {
return new Thing;
}
Thing * things[] = {
NULL
};
void setup() {
things = (Thing*[]){
MakeThing(),
NULL
};
}
However, this gets me a error: incompatible types in assignment of 'Thing* [2]' to 'Thing* [1]'
I tried:
Thing* _things[] {
MakeThing(),
NULL
};
things = _things;
Same error.
I tried:
things = new Thing*[] {
MakeThing(),
NULL
};
Same error.
I tried:
things = new Thing** {
MakeThing(),
NULL
};
which gets a error: cannot convert '<brace-enclosed initializer list>' to 'Thing**' in initialization
I tried:
things = new Thing*[] {
MakeThing(),
NULL
};
which gets error: incompatible types in assignment of 'Thing**' to 'Thing* [1]'
What's going on that this doesn't work? How can I make it work?