title: Pass a pointer to struct and a int to a function in C to implement a stack
body: <p>I'm trying to implement a stack in C. I have only implemented the struct that will contain an array and that currently only contains the size of the array and the position of the last item added to the stack</p>
  
  <p>This is a partial implementation that is giving me some trouble. </p>
  
  <p>in stack.h</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdbool.h&gt;
  
  typedef struct Stack
  {
      int max_size;
      int top;
      // int *contents;
  } Stack;
  
  Stack *stack_create(int n);
  bool stack_is_empty(Stack *stack);
  bool stack_is_full(Stack *stack);
  void stack_push(Stack *stack, int value);
  </code></pre>
  
  <p>in stack.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #ifndef STACK_H
  #include "stack.h"
  #endif
  
  Stack *stack_create(int n)
  {
      Stack stack;
      Stack *s = &amp;stack;
      s-&gt;max_size = n;
      s-&gt;top = 0;
      // s-&gt;contents = (int *)malloc(sizeof(int) * n);
      return s;
  }
  
  
  bool stack_is_empty(Stack *stack)
  {
      if (stack-&gt;top == 0)
      {
          return true;
      }
      return false;
  }
  
  bool stack_is_full(Stack *stack)
  {
      if (stack-&gt;top == stack-&gt;max_size)
      {
           return true;
      }
      return false;
  } 
  
  void stack_push(Stack *stack, int value)
  {
  
       if (!stack_is_full(stack))
       {
            printf("max_size: %d\n", stack-&gt;max_size);
            printf("top: %d (%p)\n", stack-&gt;top++, &amp;stack-&gt;top);
            printf("value: %d (%p)\n", value, &amp;value);
       }
       else
       {
            printf("Can't push. max_size==%d reached.\n", stack- &gt;max_size);
            exit(EXIT_FAILURE);
       }
  }
  </code></pre>
  
  <p>and in main.c:</p>
  
  <pre><code> #include &lt;stdio.h&gt;
   #include &lt;stdlib.h&gt;
   #include "stack.h"
  
   #define SIZE 3
  
   int main()
   {
       Stack *s = stack_create(SIZE);
       printf("stack_is_empty: %d\n", stack_is_empty(s));
       stack_push(s, 100);
       printf("stack_is_empty: %d\n", stack_is_empty(s));
       stack_push(s, 30);
       printf("stack_is_empty: %d\n", stack_is_empty(s));
       stack_push(s, 20);
       printf("stack_is_empty: %d\n", stack_is_empty(s));
  
       return 0;
   }
  </code></pre>
  
  <p><code>main</code> produces the following output:</p>
  
  <pre><code>stack_is_empty: 1
  max_size: 3
  top: 100 (0x7ffd5430dfb4)
  value: 101 (0x7ffd5430dfb4)
  stack_is_empty: 0
  max_size: 3
  top: 30 (0x7ffd5430dfb4)
  value: 31 (0x7ffd5430dfb4)
  stack_is_empty: 0
  max_size: 3
  top: 20 (0x7ffd5430dfb4)
  value: 21 (0x7ffd5430dfb4)
  stack_is_empty: 0
  </code></pre>
  
  <p>Why is <code>value</code>'s address the same of <code>stack-&gt;top</code>?</p>