title: C++ Defining the elements of an array of objects
body: <p>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, <code>class A</code> and <code>struct B</code> are declared in a header file. The way I understand this is that the statement <code>b.arr[2]</code> instantiates the array <code>b.arr</code> to be of length 2. However when I run the following code as is, I get the error <code>exited with non-zero status</code>. 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.</p>
  
  <pre><code>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] = &amp;a1;
      b.arr[1] = &amp;a2; // RUNS FINE WITHOUT THIS LINE
  }
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>By the way I am writing this for an arduino library, so I cannot use the std namespace.</p>