C++ Defining the elements of an array of objects -


i java programmer working on first project in c++ , struggling understand how arrays handled. trying write program structurally equivalent following code. in actual project however, class a , struct b declared in header file. way understand statement b.arr[2] instantiates array b.arr of length 2. when run following code is, error exited non-zero status. when comment out line intended set b.arr[1] equal a2, runs fine. why , how might this? also, there way can redefine b.arr array of different length? i've looked around nothing can find seems address specific problem.

class {};  struct b {        *arr[]; };  int main() {     b b;     b.arr[2]; // instantiates array of length 2?     a1;     a2;     b.arr[0] = &a1;     b.arr[1] = &a2; // runs fine without line } 

thanks!

by way writing arduino library, cannot use std namespace.

b.arr[2]; // instantiates array of length 2? 

that not correct.

it references third element of array , nothing value of element.

the program invokes undefined behavior since accessing elements of b.arr when not initialized point valid memory.


Comments