title: Char Arrays and Segmentation Fault
body: <p>New to C and pointers, and I can't figure out why I have a segmentation fault here...some of the code here is unused, I'm just trying to test whether or not my words are being read in properly and having the correct amount of space allocated.  Eventually I'll have a char ** where the format when printed would look like "cat the", "new hey", .....<br>
  Words, in my code, represent each individual word</p>
  
  <pre><code>#include&lt;stdlib.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;ctype.h&gt;
  
  char *expand(char* source, int size);
  
  
  char *expand(char *source, int size)
  {
    char *expansion = (char *)malloc(size * sizeof(char));
    int i;
    for (i=0; i&lt;size-1; i++)
      {
        expansion[i] = source[i];
      }
    free(source);
    return expansion;
  }
  
  int main(int argc, char **argv)
  {
    int x;
    if (argc &lt;= 2)
      {
        if (isdigit(*argv[1]))
          {
            x = atoi(argv[1]);
          }
      }
    else
      {
        fprintf(stderr, "Invalid Input\n");
        return 1;
      }
  
  
    //store pointers to each individual word
    char *words = (char *)malloc(3 * sizeof(char));
    int size = 3;//initial arbitrary size
    int count = 0;
    char *temp;
    while (1)
      {
        fscanf(stdin, "%s", temp);
        if (feof(stdin))
          {
            break; //break if end of file
          }
        if (count == size - 1)
          {
            size++;
            words = expand(words, size);
          }
        words[count] = *temp;
        count++;
      }
  
    int i;
    for(i=0; i&lt;size-1; i++)
      {
        fprintf(stderr, "%s, ", words[i]);
      }
    fprintf(stderr, "\n");
  
    return 0;
  }
  </code></pre>