Hello forum, I have what I think is a simple question that is causing me unexpectedly large headaches.
I want to pass an array to a class. In my naive mind, an array is passed by passing (by value) a pointer to its first element. So, the idea goes, the elements of an array declared in a different scope, will be accessible to the methods of the class. The code below explodes:
class classy {
public:
int a1d[]; // this should end up pointing to the array myArray in setup()
classy(int passedArray[]) {
a1d = passedArray; // this is the line that blows up
}
};
void setup() {
int myArray[5] = { 0, 1, 2, 3, 4 };
classy c = classy(myArray);
}
void loop() {}
So, I need to make it so that a method in
classy referring to, say,
a1d[2] will access
myArray[2].
However, I get the following error:
sketch_aug05c.cpp: In constructor ‘classy::classy(int*)’:
sketch_aug05c:7: error: incompatible types in assignment of ‘int*’ to ‘int [ 0 ]’So, if I got that right,
passedArray[] is considered an integer pointer (‘int*’, which is what I think a reference to an array is. Note that the constructor is referred to as ‘classy::classy(int*)’ whereas I have declared it as ‘classy(int passedArray[]’), which I guess amounts to the same thing) but
a1d is considered the contents of the first element of an integer array (‘int [ 0 ]’)? Both are referred to in exactly the same way in the offending line. Am I going crazy or is the compiler linguistically challenged?