C++ - Values change unexpectedly
<p>For some reason, the values of a passed struct change without me accessing them. I found this out by debugging with Visual Studio. Here is the code:</p>

<pre><code>bool Screen::blitSurface(SDL_Surface* src, int x, int y, SDL_Rect* rect) {
  SDL_Rect* offset;
  offset-&gt;x = x;
  offset-&gt;y = y;

  SDL_BlitSurface( src, rect, screen, offset );

  return true;
}
</code></pre>

<p>The struct that is changing is 'rect'. When it is passed, rect->x is 0 and rect->y is 16. However, at the next breakpoint (offset->x = x;) rect->x is a random number (for instance, -13108), same with rect->y. Does anybody have an idea as to why this happens? I'm completely lost, and have no idea where to start.</p>

<p>--EDIT--</p>

<p>Ok, I updated the code to this, but it's still having the problem.</p>

<pre><code>bool Screen::blitSurface(SDL_Surface* src, int x, int y, SDL_Rect* rect) {
  SDL_Rect offset;
  offset.x = x;
  offset.y = y;

  SDL_BlitSurface( src, rect, screen, &amp;offset );

  return true;
}
</code></pre>

<p>However, it did get me thinking. If I create a local struct, and return a pointer to it, it is deleted once the function completes? For example, does this work?</p>

<pre><code>SDL_Rect* getRect(int x, int y, int width, int height) {
  SDL_Rect rect;

  rect.x = x;
  rect.y = y;
  rect.w = width;
  rect.h = height;

  return &amp;rect;  
}
</code></pre>

<p>This is where I get the 'rect' variable that I am passing to 'blitSurface'.</p>
