title: Allocate struct on the heap in C
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct myStruct
  {
      int number;
  };
  
  void
  allocateMem(struct myStruct *struct1)
  {
      struct1 = malloc(sizeof(struct myStruct));
      struct1-&gt;number = 500;
      printf("struct1-&gt;number: %d\n", struct1-&gt;number);
  }
  
  int
  main(int argc, char *argv[])
  {
      struct myStruct *struct1 = NULL;
      allocateMem(struct1);
      printf("number: %d\n", struct1-&gt;number);
      return 0;
  }
  </code></pre>
  
  <p>Hello citizens of StackOverflow, I seek your assistance.</p>
  
  <p>What I am trying to do is allocate a struct using heap space rather than stack space; and I am trying to do the allocation inside of a function. No matter how much I mess with this I always get either a segmentation fault or several compiler warnings.</p>
  
  <p>I'm doing something here incorrectly, but I can't figure out what, I've been all over StackOverflow and the general internet trying to find an example or post that relates to my issue but I have not found anything.</p>
  
  <p>If you could tell me what I'm doing wrong, or just point me in the right direction I would be extremely grateful. </p>
  
  <hr>
  
  <p>Thank you everyone for your quick and accurate replies. You were correct, I needed a pointer to a pointer, my code works now thanks to your help.</p>