title: Accessing array from struct crashes the program
body: <p>I've just started programming in C++ and I'm having a problem with accessing an array from a structure that I defined.</p>
  
  <p>I'm using OpenGL to draw an array of pixel data to the screen using a texture. The array of pixel data is stored in an Image structure: </p>
  
  <pre><code>struct Image {
  
  GLubyte *pixels;
  int width;
  int height;
  int bitrate;
  
  Image(int w, int h, GLubyte* pixels, int bitrate){
      this-&gt;width = w;
      this-&gt;height = h;
      this-&gt;pixels = pixels;
      this-&gt;bitrate = bitrate;
  }
  
  };
  </code></pre>
  
  <p>The image is initialized like so:</p>
  
  <pre><code>GLubyte* pix = new GLubyte[WIDTH*HEIGHT*3];
  Image image(WIDTH, HEIGHT, pix, 3);
  </code></pre>
  
  <p>There is a separate structure called Screen which is initialized using an Image instance:</p>
  
  <pre><code>    struct Screen{
  
      int width, height;
      Image* screen;
  
      void setScreen(Image* screen, const int width, const int height){
          this-&gt;screen = screen;
          this-&gt;width = width;
          this-&gt;height = height;
      }
  
  };
  </code></pre>
  
  <p>The screen is initialized like so (right after the Image declaration):</p>
  
  <pre><code>displayData = new Screen();
  displayData-&gt;setScreen(&amp;image, WIDTH, HEIGHT);
  </code></pre>
  
  <p>When I access the Width or Height variables it gets the correct value. I then pass the screen instance to this method:</p>
  
  <pre><code>void renderScreen(Screen *screen){
  std::cout &lt;&lt; "Rendering Screen" &lt;&lt; std::endl;
  for(int x = 0; x &lt; screen-&gt;width; x++){
      for(int y = 0; y &lt; screen-&gt;height; y++){
  
          std::cout &lt;&lt; "rendering " &lt;&lt; x &lt;&lt; " " &lt;&lt; y &lt;&lt; std::endl;
  
                      //Gets here and crashes on x - 0 and y - 0
  
          screen-&gt;write(0xFFFFFFFF, x, y);
      }
  }
  std::cout &lt;&lt; "done rendering Screen" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>which is called like this:</p>
  
  <pre><code>render(displayData); (displayData is a Screen pointer)
  </code></pre>
  
  <p>Pointers are quite new to me and I don't know rules about passing pointers to structures or anything like that.</p>