c语言 - " 'char *' but the argument has type 'char (*)[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);
}

警告:format指定类型"char",但参数的类型为"char(([1]"[-Wformat]scanf("%s",&文件名(;~~~~^~~~~~

箭头转到"&文件名"。

如何修复?非常感谢。

scanf()期望char*代替%s

File_name具有类型char[1],因为它是一个元素数组并且该元素被初始化为''

表达式中的大多数数组都转换为指针,但一元&的操作数除外(本例(。

因此,&File_name成为指向数组的指针,并且其类型为char(*)[1]

要修复此问题,请删除File_name之前的&。然后,数组File_name将被转换为指向其第一个元素的char*

还有:

  • 1元素肯定太短,无法读取字符串。通过指定char File_name[512] = "";等元素的数量来分配更多的元素
  • CCD_ 15是一个多字符字符常量。它的价值是由实现定义的,不会是您想要的。您应该使用strstr(File_name,".txt")而不是strchr(File_name,'.txt')。(strstr用于搜索字符串(包括中间(,而不是检查后缀,但它的性能会比strchr()好(

相关内容

最新更新