title: How to dynamically allocate memory space for a string and get that string from user?
body: <p>I want to read input from user using C program. I don't want to use array like,</p>
  
  <pre><code>char names[50];
  </code></pre>
  
  <p>because if the user gives string of length 10, then the remaining spaces are wasted.</p>
  
  <p>If I use character pointer like,</p>
  
  <pre><code>char *names;
  </code></pre>
  
  <p>then I need to allocate memory for that in such a way of,</p>
  
  <pre><code>names = (char *)malloc(20 * sizeof(char));
  </code></pre>
  
  <p>In this case also, there is a possibility of memory wastage.</p>
  
  <p>So, what I need is to dynamically allocate memory for a string which is of exactly same as the length of the string.</p>
  
  <p>Lets assume,</p>
  
  <p>If the user input is <code>"stackoverflow"</code>, then the memory allocated should be of <code>14</code> (i.e. Length of the string = 13 and 1 additional space for '\0').</p>
  
  <p>How could I achieve this?</p>