title: Why do I get a segmentation fault when using strncpy?
body: <p>I am getting segmentation fault when using strncpy and (pointer-to-struct)->(member) notation:</p>
  
  <p>I have simplified my code. I initialise a struct and set all of it's tokens to an empty string. Then a declare a pointer to a struct and assign the address of the struct to it.</p>
  
  <p>I pass the pointer to a function. I can print out the contents of the struct at the beginning of the function, but if I try to use the tp -> mnemonic in a strncpy function, I get seg fault.  Can anyone tell me what I am doing wrong?</p>
  
  <pre><code>typedef struct tok  {
      char* label;
      char* mnem;
      char* operand;
  }Tokens;
  
  Tokens* tokenise(Tokens* tp, char* line)  {
      // This prints "load"
      printf("Print this - %s\n", tp -&gt; mnem);
  
      // This function gives me segmentation fault
      strncpy(tp -&gt; mnem, line, 4);
  
      return tp;
  }
  
  int main()  {
      char* line = "This is a line";
      Tokens tokens;
      tokens.label = "";
      tokens.mnem = "load";
      tokens.operand = "";
  
      Tokens* tp = &amp;tokens;
      tp = tokenise(tp, line);
  
      return 0;
  }
  </code></pre>
  
  <p>I have used printf statements to confirm that the code definitely stops executing at the strncpy function.</p>