title: Why am I getting Segmentation fault (core dumped) or bus error (core dumped) when trying to populate a struct?
body: <p>So I am trying to use a pointer to a struct of <code>MonsterAttacks</code> as the data that belongs to an element of a linked list. In order to do this I try to populate a struct of <code>MonsterAttacks</code> and then pass that along with a null ptr to a next node to a function called create. However somewhere in the <code>populate</code> method a segmentation fault error occurs. I am working with three files <code>list_demo.c</code>, <code>linked_list.h</code> and <code>linked_list.c</code>. I will build all the the functions that make up a fully functioning linked list, well hoping I can as soon as I get pass this error. Been dealing with this error for about two days and I showed my professor and he could not figure out why its happening, it seems to come from the <code>populate</code> function. I have tried to return a pointer to a strut in which case I get a bus error, and I have tried almost every variation of getting input and storing it on the strut. I even deleted the function and tried to populate it in main, but nothing works. I am new to C and my professor helped me out for about an hour debug this problem and he finally gave up, so any help would be appreciated.</p>
  
  <p><strong>list_demo.c</strong></p>
  
  <pre><code> #include &lt;stdio.h&gt;
   #include "linked_list.h"
   #include &lt;stdlib.h&gt;
  
  
  
   void populate(struct MonsterAttacks *m){
  
      printf("Enter the name for the Monster \n");
      scanf("%40s",m-&gt;monsterName);
      puts("What is his/her attack location?");
      scanf("%40s",m-&gt;attackLocation);
      puts("What are the number of victims this monster has demolished?");
      scanf("%ud", &amp;m-&gt;numOfVictims);      
      m-&gt;attackID = 0;
  }
  
  int main(void)
  {
  
     node* tmp = NULL;
     struct MonsterAttacks *tmpMonst = (struct MonsterAttacks *) 
     malloc(sizeof(struct MonsterAttacks));
  
     if(tmpMonst == NULL){
        printf("Error allocating memory");
     }
     else
        populate(tmpMonst);
  
     node *head = create(tmpMonst,tmp);
  
     free(tmpMonst);
     return 0;
  }
  </code></pre>
  
  <p><strong>linked_list.h</strong></p>
  
  <pre><code>#ifndef LINKED_LIST
  #define LINKED_LIST
  
  
  typedef struct node{
     struct MonsterAttacks *monsterAttack;
     struct node* next;
  } node;
  
  
  struct MonsterAttacks{
     unsigned int attackID;
     char monsterName[41];
     char attackLocation[41];
     unsigned int numOfVictims;
  };
  
  /*
     create a new node
     initialize the data and next field
  
     return the newly created node
  */
  node* create(struct MonsterAttacks *m,node* next);
  
  
  
  #endif
  </code></pre>
  
  <p><strong>linked_list.c</strong></p>
  
  <p>// from zentut.com, heavily adapted</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include "linked_list.h" 
  
  
  /*
     create a new node
     initialize the data and next field
     return the newly created node
  */
  node* create(struct MonsterAttacks *m,node* next)
  {
      node* new_node = (node*)malloc(sizeof(node));
      if(new_node == NULL)
      {
          printf("Error creating a new node.\n");
          exit(0);
      }
  
       new_node-&gt;monsterAttack-&gt;attackID = 0;
  
       new_node-&gt;next = next;
  
       strncpy(new_node-&gt;monsterAttack-&gt;monsterName,m-&gt;monsterName,41);
       strncpy(new_node-&gt;monsterAttack-&gt;attackLocation, m-&gt;attackLocation, 41);
       new_node-&gt;monsterAttack-&gt;numOfVictims = m-&gt;numOfVictims;
  
        return new_node;
  }
  </code></pre>
  
  <p><strong>Btw running on Red Hat using gcc compiler</strong></p>