title: Delete struct from stack memory
body: <p>I have a linked list struct, i want to pass one node (another struct) pointer to a function (the node is part of the linked list, but i'm passing the node seperately to a deleter function</p>
  
  <p>I want it to copy the next node data into itself (overriding its data), and to delete the next node, thus deleting itself (this part is working)..
  I made it check whether the passed node is the last node in the list, and if so, to delete itself.</p>
  
  <p>I don't know how to delete structs from the stack (i know i can malloc() and free() it using the heap memory).</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  typedef struct {
      int data;
      struct node * next;
  }node;
  
  typedef struct {
      struct node * head;
  }linked_list;
  
  void print_list(linked_list * list) {
  
      node *current = list-&gt;head;
      while (current) {
          printf("Current node has %d\n",current-&gt;data);
          current = current-&gt;next;
      }
  }
  
  void delete_node(node * n) {
      node * next = n-&gt;next;
      if (next) {
          n-&gt;data = next-&gt;data;
          n-&gt;next = next-&gt;next;
      }
      else {
          *n =  NULL; /*This of course won't compile because assigning void* (null) to node variable
                                but if i make n point to NULL, nothing will happen because i'm inside a function
                                and the pointer is duplicated (the passed pointer will still work) */
      }
  }
  
  void main(){
      node first;
      node second;
      first.data = 1;
      first.next = &amp;second;
      second.data = 2;
      second.next = NULL;
      linked_list l;
      l.head = &amp;first;
      print_list(&amp;l);
      delete_node(&amp;second);
      print_list(&amp;l);
  }
  </code></pre>