title: C - Changing the value of a variable outside of a function by passing pointers
body: <p>I have an assignment in C to implement a abstract data type STACK. The nature of the data type requires key structure that needs to have memory allocated. My problem is that my instructor insists, for now, for the initialization function to take in a pointer to the key structure. The init() function will do nothing more than allocate the memory necessary for the structure and set a field to zero, but the pointer that is passed in needs to be assigned that memory location. </p>
  
  <p>I can't think of a way to do this without either having the function return a pointer, or to pass in a 2-star pointer - both of which are not allowed. I know  The function prototype must be (where stackT* is a pointer to the key STACK data structure):</p>
  
  <pre><code>    void init(stackT* stack);
  </code></pre>
  
  <p>I came up with this and it works fine:</p>
  
  <pre><code>    void init(stackT** stack){
          *stack = (stackT*) malloc(sizeof(stack));
          (*stack)-&gt;count = 0;
          return;
      }
  </code></pre>
  
  <p>But it does not abide by the restrictions of the assignment. </p>
  
  <hr>
  
  <p>tl;dr version:</p>
  
  <p>Basically, how can I pass in the address of my original pointer to the STACK data structure (&amp;stackPtr) into a function that takes one-star pointers as arguments and not get a pointer-type warning? Further, once you change the arguments to <code>(stackT* stack)</code> the below code does not work, even though I am passing the same thing either way - this is where my problem is. </p>
  
  <p>I thought it is REQUIRED to have the argument as a 2-star pointer if you intend to pass in a pointer to a pointer .. the compiler must know what it is dealing with when you dereference a pointer.</p>
  
  <p>At any rate, I am not sure how to do this given the restrictions. In my opinion this is only making it unnecessarily more difficult. </p>