title: How to pass a formatted string to a struct in C?
body: <p>Referencing a similar question at <a href="https://stackoverflow.com/questions/38670719/splitting-a-string-into-elements-of-a-struct">Splitting a string into elements of a struct</a>.</p>
  <p>In a part of my project, I have been entering into <code>signal: segmentation fault (core dumped)</code>. My task is to extract a formatted string from a file (which is a line of a file), and convert it into a struct. For example, I have already extracted a string &quot;t,a,1990&quot; and I would like to use <code>sscanf</code> to generate a struct <code>s</code> where <code>s.title</code> is &quot;<code>t</code>&quot; and so on. I noticed that in the struct given, title and artist are both pointers. I am new to the language and I am wondering why passing a pointer (<code>s.title</code> and <code>s.artist</code>) to a pointer is invalid here.</p>
  <p>How can I solve it without changing the struct def?</p>
  <p>Here is my code:</p>
  <pre><code>#include  &lt;stdio.h&gt;
  
  typedef struct {
    char* title;
    char* artist;
    int year;
  } song;
  
  int main(void) {
  
      song s;
  
      // read from string
  
      char str[]= &quot;t,a,1990&quot;;
      char* strptr = str;
  
      sscanf(strptr, &quot;%s,%s,%d&quot;, s.title, s.artist, &amp;(s.year));
      printf(&quot;title = %s, artist = %s, year= %d\n&quot;, s.title, s.artist, s.year);
  
      return 0;
  }
  </code></pre>
  <p>Thank you!</p>