title: Pointer changes value after printf
body: <p>So today I was trying to implement simple stack in C, but I have encountered following issue that I can't resolve or explain why is happening.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  typedef struct stack_element stack_element;
  typedef struct stack stack;
  
  struct stack_element
  {
      void* data;
      stack_element* next;
  };
  
  struct stack
  {
      stack_element* top;
      int size;
      int max_size;
  };
  
  void push(stack* this, stack_element to_add)
  {
      if(this-&gt;top == NULL)
      {
          this-&gt;top = &amp;to_add;
          this-&gt;top-&gt;next = this-&gt;top;
          printf("%p\n", this-&gt;top);
          printf("%p\n", this-&gt;top-&gt;next);
          printf("First\n");
          return;
      }
  }
  
  void debug(stack* this)
  {
      if(this-&gt;top == NULL) return;
      printf("Sample 1 %p\n", this-&gt;top);
      printf("Sample 2 %p\n\n", this-&gt;top-&gt;next);
  }
  
  int main()
  {
      stack_element* tmp = malloc(sizeof(stack_element));
      stack* st = malloc(sizeof(stack)); 
      tmp-&gt;data = 1;
      push(st, *tmp);
      printf("Sample 1 %p\n", st-&gt;top);
      printf("Sample 2 %p\n\n", st-&gt;top-&gt;next);
      debug(st);
      printf("Sample 1 %p\n", st-&gt;top);
      printf("Sample 2 %p\n\n", st-&gt;top-&gt;next);
      printf("END\n");
      return 0;
  }
  </code></pre>
  
  <p>Code above gives some strange results, and also different ones if I change compiler.</p>
  
  <p>First compiler I've tried is gcc and it gives following output:</p>
  
  <pre><code>0x7ffd6d96d3b0
  0x7ffd6d96d3b0
  First
  Sample 1 0x7ffd6d96d3b0
  Sample 2 0x7ffd6d96d3b0
  
  Sample 1 0x7ffd6d96d3b0
  Sample 2 0x400670
  
  Sample 1 0x7ffd6d96d3b0
  Sample 2 0x40068d
  
  END
  </code></pre>
  
  <p>Second result is from clang:</p>
  
  <pre><code>0x7ffd6d05fb30
  0x7ffd6d05fb30
  First
  Sample 1 0x7ffd6d05fb30
  Sample 2 0x7ffd6d05fb30
  
  Sample 1 0x7ffd6d05fb30
  Sample 2 0x2042030
  
  Sample 1 0x7ffd6d05fb30
  Sample 2 0x2042030
  
  END
  </code></pre>
  
  <p><strong>My first question is why is Sample 2 even changing when I printf it from void debug?</strong> Also I have tried to comment out debug function call and results are also strange. In gcc samples are as expected all are matching, but in clang there is difference in sample 2 for no obvious reason.</p>
  
  <p><strong>My second question is why is there even difference between compilers while compiling this code?</strong></p>
  
  <p>Hope I have posted enough information about the issue I am having, if not write me in comment to post more information.</p>