title: A function in C that adds a node at the end of a singly linked list is not working
body: <p>I just wrote a function in C that is supposed to add a node at the end of a linked list, but when I compiled the program, I got nothing in the console. Here is the body of the function :</p>
  <pre><code>void addAtend(node *head, int val){
  node *temp;
  node *tempVal;
  temp =head;
  tempVal= (node*)malloc(sizeof(node));
  if(tempVal==NULL){
      printf(&quot;Error! Memory was not allocated!&quot;);
      exit(-1);
  }
  tempVal -&gt;data=val;
  tempVal-&gt;next=NULL;
  while (temp!=NULL)
  {
      temp = temp-&gt;next;
  
  }
  temp-&gt;next = tempVal;
  </code></pre>
  <p>}</p>
  <p><strong>Here is the complete C program:</strong></p>
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  typedef struct linked {
      int data;
      struct linked *next;
  
  
  } node;
  
  //function to ietrate through a linked list
  void printer(node *head){
      node *temp = head;
      while (temp!=NULL)
      {
          printf(&quot;%d &quot;, temp-&gt;data);
          temp = temp-&gt;next;
      }
      
  }
  
  //function to add a value to the end of the linked list
  void addAtend(node *head, int val){
      node *temp;
      node *tempVal;
      temp =head;
      tempVal= (node*)malloc(sizeof(node));
      if(tempVal==NULL){
          printf(&quot;Error! Memory was not allocated!&quot;);
          exit(-1);
      }
      tempVal -&gt;data=val;
      tempVal-&gt;next=NULL;
      while (temp!=NULL)
      {
          temp = temp-&gt;next;
  
      }
      temp-&gt;next = tempVal;
      
  
  
  }
  
  int main(){
  
      node *ptr = (node*)malloc(sizeof(node));
      if(ptr==NULL){
          printf(&quot;Error!&quot;);
          exit(-1);
      }
      node *head;
      head = ptr;
      
      ptr-&gt;data = 30;
      ptr-&gt;next = (node*)malloc(sizeof(node));
      ptr-&gt;next-&gt;data =50;
      ptr-&gt;next-&gt;next = NULL;
  
  
      addAtend(head, 40);
      printer(head);
  
  }
  </code></pre>
  <p><strong>The output looks like this:</strong>
  <a href="https://i.stack.imgur.com/QCd2R.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>Could anyone look at this code and tell me what's wrong with function?</p>