-1
FILE *fd;
char File_name[]="";
<...>
printf("Enter the name of the file where you want the results to be saved. \n");
    printf("DON'T FORGET that file must end with .exe \n");
    scanf("%s",&File_name);
    while(strchr(File_name,'.txt')==NULL)
    {
        printf("The end of the file name is not correct. Please try again. \n");
        printf("File name: ");
        scanf("%s",&File_name);
    }

Warning: format specifies type 'char ' but the argument has type 'char ()[1]' [-Wformat] scanf("%s",&File_name); ~~~~^~~~~~~~~~

Arrow goes to "&File_name".

How to fix it? Thank you.

Max_insane
  • 23
  • 1
  • 5
  • 1
    Aside: `char File_name[]="";` is not big enough to input *anything*. it is an array of length `1` which can hold only the nul string terminator. – Weather Vane Nov 03 '20 at 10:49
  • The compiler is saying that `scanf("%s",&File_name);` should be `scanf("%s",File_name);` An array passed to a function decays to a pointer, so you don't need the indirection. – Weather Vane Nov 03 '20 at 10:50
  • But I have empty char, and user write file name and somehow program should put that saved file name into char. Or maybe you have other ideas? – Max_insane Nov 03 '20 at 10:53
  • 1. Compiler says "boohoo this line is bad: `scanf("%s",&File_name);` Specifically, the bug is here ``" . 2. Stare intently at that line. Could it actually be bad like the compiler says? 3. You don't actually need to understand what the compiler error means to find yet another `&` goof-up bug. There's an arrow pointing out the bug for you, how much more obvious can it get? – Lundin Nov 03 '20 at 10:54
  • `&File_name` => `File_name` – 0___________ Nov 03 '20 at 11:13
  • Does this answer your question? [scanf a string in a char pointer](https://stackoverflow.com/questions/37921081/scanf-a-string-in-a-char-pointer) – Mad Physicist Dec 09 '20 at 22:10

1 Answers1

1

scanf() expects char* for %s.

File_name has type char[1] because it is one-element array and the element is initialized to '\0'.

Most arrays in expressions are converted to a pointer, but one of the exception is an operand of unary & (this case).

Therefore, &File_name becomes a pointer to the array and its type is char(*)[1].

To fix, remove the &s before File_name. Then the array File_name will be converted to char* pointing at its first element.

Also:

  • 1 element is definitely too short to read strings. Allocate more elements by specifying number of elements like char File_name[512] = "";.
  • '.txt' is a multi-character character constant. Its value is implementation-defined and will not be what you want. You should use strstr(File_name,".txt") instead of strchr(File_name,'.txt'). (strstr is for searching for strings (including middle), not for checking suffix, but it will behave better than strchr()).
MikeCAT
  • 73,922
  • 11
  • 45
  • 70