title: Why does my C code print out different values when referring to the same variable?
body: <p>Here's goes my first attempt at a question on stackoverflow -</p>
  <p>I have the following implemented in C:</p>
  <p>When running my program, I have realized that the value of hashstring printed out differs in the two following functions:</p>
  <pre><code>void foo() {
      ...
      Obj *obj = Obj(args);
      char *hashstring = getHash(obj);
      printf(&quot;My value of hashstring: %s\n&quot;, hashstring);
      ...
  }
  
  char* getHash(Obj *obj) {
      ...
      int hashint = getHashInt(args);
      // I am trying to do a conversion from int to string here
      char hashstring[12]; 
      sprintf(hashstring, &quot;%d&quot;, hashint);
      printf(&quot;My value of hashstring: %s\n&quot;, &amp;hashstring);
      return &amp;hashstring;
  }
  
  </code></pre>
  <p><em>Here's what gets printed out:</em> <br />
  My value of hashstring: 1001 // printing in getHash: this is the expected <br />
  My value of hashstring: 0 <br />
  My value of hashstring: 2002 // printing in getHash: this is the expected <br />
  My value of hashstring: 0 <br />
  My value of hashstring: 3003 // printing in getHash: this is the expected <br />
  My value of hashstring: 0 <br /></p>
  <p>I have tried debugging by printing the pointer of hashstring instead, in foo(). It seems like in every single call, I am getting the same address for hashstring.</p>
  <p><em>Here's what gets printed out:</em> <br />
  0x7ffee1992f54 <br />
  0x7ffee1992f54 <br />
  0x7ffee1992f54 <br /></p>
  <p>I guessed that this probably meant that I am somehow referencing the same hashstring variable in getHash(), which is strange since I am creating a new variable each time.  I have also tried other variants like snprintf(hashstring, 12, &quot;%d&quot;, hashint) in getHash(), or creating a char *pointerToHash and passing in *pointerToHash to sprintf.</p>
  <p>Any ideas?</p>