为什么 gcc 会给我警告?
Why gcc gives to me warnings?
为什么我用 gcc 编译时会收到一些关于编译代码的警告?
我试图更改代码的某些部分以抑制警告,但是当我在编译时抑制警告然后在运行时出现错误并且调试会话停止。
欢迎任何帮助理解为什么会发生这种情况。
我收到的警告如下:
gcc Question_1.c -o Question_1
Question_1.c: In function ‘main’:
Question_1.c:43:10: warning: passing argument 1 of ‘menu’ from incompatible pointer type [-Wincompatible-pointer-types]
menu(&name_file_in,&name_file_out);
^
Question_1.c:5:6: note: expected ‘char *’ but argument is of type ‘char (*)[17]’
void menu(char *name_file_in,char *name_file_out)
^~~~
Question_1.c:43:24: warning: passing argument 2 of ‘menu’ from incompatible pointer type [-Wincompatible-pointer-types]
menu(&name_file_in,&name_file_out);
^
Question_1.c:5:6: note: expected ‘char *’ but argument is of type ‘char (*)[16]’
void menu(char *name_file_in,char *name_file_out)
^~~~
生成它的C代码如下:
#include <stdio.h>
#include <string.h>
void menu(char *name_file_in,char *name_file_out)
{
int option;
do
{
printf("\n1. To write a name for an input file.");
printf("\n2. To write a name for an output file.");
printf("\n3. Exit.");
printf("\n\nWrite an option (1-3): ");
scanf("%d",&option);
switch(option)
{
case 1: printf("\nWrite a name for the input file, (1 default): ");
scanf("%s",&*name_file_in);
if(strcmp(&*name_file_in,"1")==0)
strcpy(&*name_file_in,"data_in.dat");
printf("\nThe input filename is: %s\n",&*name_file_in);
break;
case 2: printf("\nWrite a name for the output file, (1 default): ");
scanf("%s",&*name_file_out);
if(strcmp(&*name_file_out,"1")==0)
strcpy(&*name_file_out,"data_out.dat");
printf("\nThe output filename is: %s\n",&*name_file_out);
break;
}
} while(option!=3);
}
int main()
{
char name_file_in[17],name_file_out[16];
menu(&name_file_in,&name_file_out);
return 0;
}
menu(&name_file_in,&name_file_out);
当您使用 &
时,您试图传递完整数组的地址,即 char (*)[17]
传递数组中第一个元素的地址,如下所示。
menu(name_file_in, name_file_out);
或
期望函数中的固定大小的数组如下所示。
void menu(char (*name_file_in)[17], char (*name_file_out)[16])
为什么我用 gcc 编译时会收到一些关于编译代码的警告?
我试图更改代码的某些部分以抑制警告,但是当我在编译时抑制警告然后在运行时出现错误并且调试会话停止。
欢迎任何帮助理解为什么会发生这种情况。
我收到的警告如下:
gcc Question_1.c -o Question_1
Question_1.c: In function ‘main’:
Question_1.c:43:10: warning: passing argument 1 of ‘menu’ from incompatible pointer type [-Wincompatible-pointer-types]
menu(&name_file_in,&name_file_out);
^
Question_1.c:5:6: note: expected ‘char *’ but argument is of type ‘char (*)[17]’
void menu(char *name_file_in,char *name_file_out)
^~~~
Question_1.c:43:24: warning: passing argument 2 of ‘menu’ from incompatible pointer type [-Wincompatible-pointer-types]
menu(&name_file_in,&name_file_out);
^
Question_1.c:5:6: note: expected ‘char *’ but argument is of type ‘char (*)[16]’
void menu(char *name_file_in,char *name_file_out)
^~~~
生成它的C代码如下:
#include <stdio.h>
#include <string.h>
void menu(char *name_file_in,char *name_file_out)
{
int option;
do
{
printf("\n1. To write a name for an input file.");
printf("\n2. To write a name for an output file.");
printf("\n3. Exit.");
printf("\n\nWrite an option (1-3): ");
scanf("%d",&option);
switch(option)
{
case 1: printf("\nWrite a name for the input file, (1 default): ");
scanf("%s",&*name_file_in);
if(strcmp(&*name_file_in,"1")==0)
strcpy(&*name_file_in,"data_in.dat");
printf("\nThe input filename is: %s\n",&*name_file_in);
break;
case 2: printf("\nWrite a name for the output file, (1 default): ");
scanf("%s",&*name_file_out);
if(strcmp(&*name_file_out,"1")==0)
strcpy(&*name_file_out,"data_out.dat");
printf("\nThe output filename is: %s\n",&*name_file_out);
break;
}
} while(option!=3);
}
int main()
{
char name_file_in[17],name_file_out[16];
menu(&name_file_in,&name_file_out);
return 0;
}
menu(&name_file_in,&name_file_out);
当您使用 &
时,您试图传递完整数组的地址,即 char (*)[17]
传递数组中第一个元素的地址,如下所示。
menu(name_file_in, name_file_out);
或
期望函数中的固定大小的数组如下所示。
void menu(char (*name_file_in)[17], char (*name_file_out)[16])