title: Reading Strings with Undefined Length in C
body: <p>first(as always) I want to apologize about my english, it may not be clear enough. </p>
  
  <p>I'm not that good at C programming, and I was asked to read a "string" input with undefined length.</p>
  
  <p>This is my solution</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  char *newChar();
  char *addChar(char *, char);
  char *readLine(void);
  
  int main() {
    char *palabra;
    palabra = newChar();
  
    palabra = readLine();
    printf("palabra=%s\n", palabra);
  
    return 0;
  }
  
  char *newChar() {
    char *list = (char *) malloc(0 * sizeof (char));
    *list = '\0';
    return list;
  }
  
  char *addChar(char *lst, char num) {
    int largo = strlen(lst) + 1;
    realloc(&amp;lst, largo * sizeof (char));
    *(lst + (largo - 1)) = num;
    *(lst + largo) = '\0';
    return lst;
  }
  
  char *readLine() {
    char c;
    char *palabra = newChar();
  
    c = getchar();
    while (c != '\n') {
      if (c != '\n') {
        palabra = addChar(palabra, c);
      }
      c = getchar();
    }
    return palabra;
  }
  </code></pre>
  
  <p>Please, I'd appreciate that you help me by telling me if it's a good idea or giving me some other idea(and also telling me if it's a "correct" use for pointers).</p>
  
  <p>Thanks in advance</p>
  
  <hr>
  
  <p><strong>EDIT:</strong> Well, thanks for you answers,they were very useful. Now I post edited(and I hope better) code, maybe could be useful for someone new to C(like me) and be feedbacked again.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  
  void reChar(char **, int *);
  void readLine(char **, int *);
  
  int main() {
      char *palabra = NULL;
      int largo = 0;
  
      reChar(&amp;palabra, &amp;largo);
      readLine(&amp;palabra, &amp;largo);
      printf("palabra=%s\n", palabra, largo);
  
      system("pause");
      return 0;
  }
  
  void reChar(char **lst, int *largo) {
      (*largo) += 4;
      char *temp = (char*) realloc(*lst, (*largo) * sizeof (char));
  
      if (temp != NULL) {
          *lst = temp;
      } else {
          free(*lst);
          puts("error (re)allocating memory");
          exit(1);
      }
  }
  
  void readLine(char **lst, int *largo) {
      int c;
      int pos = 0;
  
      c = getchar();
      while (c != '\n' &amp;&amp; c != EOF) {
          if ((pos + 1) % 4 == 0) {
              reChar(lst, largo);
          }
          (*lst)[pos] =(char) c;
          pos++;
          c = getchar();
      }
      (*lst)[pos] = '\0';
  }
  </code></pre>
  
  <p><strong>PS:</strong> </p>
  
  <ul>
  <li><p>It seem enough to slow increase size of "palabra".      </p></li>
  <li><p>I'm not sure if capture <code>getchar()</code> into a <code>int</code> and then cast it into a <code>char</code> is the correct way to hadle <a href="http://en.wikipedia.org/wiki/C_file_input/output#EOF_pitfall" rel="nofollow noreferrer">EOF pitfall</a></p></li>
  </ul>