title: Taking string input using pointers in c gives me segmentation fault in one function. Same block of code works fine for another function?
body: <p>So I want to take inputs of names as strings using char* pointers.
  I looked online and found a solution by giving my pointer memory using malloc and then taking the input using <code>scanf</code>, as shown below: </p>
  
  <pre><code>  char *userInput = (char*)malloc(20 * sizeof(char));
    printf("\nEnter a name: ");
    scanf("%s",userInput);
    char *name = (char*)malloc(getLength(userInput)* sizeof(char));
    name = userInput;
    if(name == NULL){
      printf("Memory not allocated");
      exit(0);
    }
    free(userInput);
  </code></pre>
  
  <p>This works, so I copied and pasted it to another function where similar input is required as below:</p>
  
  <pre><code>  char *userInput = (char*)malloc(20 * sizeof(char));
    printf("\nEnter a name: ");
    scanf("%s",userInput);
    char *searched = (char*)malloc(getLength(searched)* sizeof(char));
    searched = userInput;
    if(searched == NULL){
      printf("Memory not allocated");
      exit(0);
    }
    free(userInput);
  </code></pre>
  
  <p>But when I run the code for this function, it gives me "exited, segmentation fault".</p>
  
  <p>Any ideas on why it doesn't work in my second function?</p>
  
  <p>EDIT: getLength() is a function that returns the length of a given string. </p>