I am a Java programmer working on my first project in C++ and I am struggling to understand how arrays are handled. I am trying to write a program that is structurally equivalent to the following code. In my actual project however, class A
and struct B
are declared in a header file. The way I understand this is that the statement b.arr[2]
instantiates the array b.arr
to be of length 2. However when I run the following code as is, I get the error exited with non-zero status
. When I comment out the line that is intended to set b.arr[1] equal to a2, it runs fine. Why is this and how might I do this? Also, is there a way I can redefine the b.arr array to be of a different length? I've looked around but nothing I can find seems to address this specific problem.
class A {};
struct B {
A *arr[];
};
int main() {
B b;
b.arr[2]; // instantiates array to be of length 2?
A a1;
A a2;
b.arr[0] = &a1;
b.arr[1] = &a2; // RUNS FINE WITHOUT THIS LINE
}
Thanks!
By the way I am writing this for an arduino library, so I cannot use the std namespace.