title: How to store a string that the user inputted into a char pointer instead of a char array
body: <p>I'm trying to build a program with C but I'm having trouble changing a char array into a char pointer and adjusting the program accordingly. Here's my current code that I want to change:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  
  char username[20];
  char password[20];
  char username_input[20];
  char password_input[20];
  char user_input;
  
  void create_account(char* usrname, char* passwd) {
      printf(&quot;==================================CREATE BANK ACCOUNT==================================\n&quot;);
      while(1) {
          printf(&quot;Enter a username that is less than 20 characters: &quot;);
          scanf(&quot;%s&quot;, usrname);
  
          if (strlen(usrname) &lt;= 20)
              break;
  
          printf(&quot;That is not less than 20 characters, try again...\n&quot;);
          sleep(2);
      }
  
      while(1) {
          printf(&quot;Enter a password that is less than 20 characters: &quot;);
          scanf(&quot;%s&quot;, passwd);
  
          if (strlen(passwd) &lt;= 20) {
              break;
          }
          printf(&quot;That is not less than 20 characters, try again... \n&quot;);
          sleep(2);
      }
  
      printf(&quot;Thank you, please sign in now...\n&quot;);
      sleep(2);
  }
  
  void login() {
      while(1) {
          printf(&quot;Enter Username: &quot;);
          scanf(&quot;%s&quot;, username_input);
          printf(&quot;Enter Password: &quot;);
          scanf(&quot;%s&quot;, password_input);
          if (strcmp(username, username_input) != 0 || strcmp(password, password_input) != 0) {
              printf(&quot;Incorrect Username or Password. Try again...\n&quot;);
              sleep(2);
          }
  
          else {
              printf(&quot;Welcome %s\n&quot;, username);
              sleep(2);
              break;
          }
      }
  }
  </code></pre>
  <p>On the lines at the beginning, you can see that there are 4 char array declarations. I want them to be char pointers like so:</p>
  <pre><code>char* username;
  char* password;
  char* username_input;
  char* password_input;
  </code></pre>
  <p>The reason for this is because I don't want a limit in a string, but arrays need limits. Once I change that, I want to use <code>malloc()</code> to allocate memory for what the user inputs but I don't know how. In other words, I want to declare a char pointer that accepts user input. And I want enough memory to be allocated for that pointer so that the string that was inputted has enough space. Also I want my code to be compatible with different compilers and computers. For that I'm pretty sure that I have to multiply the <code>malloc()</code> function with <code>sizeof(char)</code> or something like that. I don't necessarily get an error, as in I don't get red lines in my IDE, but the program stops in the middle of it for no reason and gives me an exit code other than 0.</p>