0

So in the following function...I want to store the string, that the user enters, into the pointer variable targetFile. I am getting the following warning and when I execute it, I get a segmentation fault. warning

My Code is below

void findFile()
{
    char *targetFile;
    printf("Enter the complete file name that you wish to search: ");
    scanf("%s", &targetFile);
    printf("\n");
    
}

I know I could have done the following, which works perfectly fine, but I don't know how large the targetFile character array will be. Just manually entering in 50 seems like bad practice. Can someone tell me the best approach? Thank you!

void findFile()
{
    char targetFile[50];
    printf("Enter the complete file name that you wish to search: ");
    scanf("%s", targetFile);
    printf("\n");
    printf("%s\n", targetFile);
    
}
  • you don't. You assign the address of a string to the pointer variable, and then read into that with `scanf()`. – Barmar Oct 16 '21 at 04:19

0 Answers0