title: C - split/store string of X length into an array of structs
body: <p>I'm trying to split a string every X amount of characters, and then store each line in an array of structs. However, I'm wondering what would be a short and efficient way of doing it. I thought that maybe I could use <code>sscanf</code>, but not very sure how to. Any help will be appreciated. So far I have:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  struct st {char *str;};
  
  int main ()
  {
     struct st **mystruct;
  
     char tmp[] = "For configuration options (arch/xxx/config.in, and all the Config.in files),somewhat different indentation is used.";
     size_t max = 20, j = 0; // max length of string
     size_t alloc = strlen(tmp)/max + 1;
  
     mystruct = malloc(alloc * sizeof *mystruct);
     for (j = 0; j &lt; alloc; j++)
        mystruct[j] = malloc(sizeof *mystruct[j]);
  
     const char *ptr = tmp;
     char field [ max ];
     int n;
  
     while (*ptr != '\0') {
          int line = sscanf(ptr, "%s", field, &amp;n); // not sure how to use max in here
          mystruct[j]-&gt;str = field;
          field[0]='\0';
          if (line == 1)
              ptr += n;
          if ( n != max )
              break;
          ++ptr;
          ++j;
      }
  
     return 0;
  }
  </code></pre>
  
  <p>So when I iterate over my struct, I can get something like:</p>
  
  <pre><code>For configuration op
  tions (arch/xxx/conf
  ig.in, and all the C
  onfig.in files),some
  what different inden
  tation is used.
  </code></pre>