警告 – 初始化使得指针来自整数而没有强制转换

我发现了类似的问题,但我不认为它们适用于我的具体问题,所以如果他们这样做我很抱歉!

我正在学习C作为第一年的CS学生,并尝试在C中进行测验,但我无法到达任何地方,因为每次我尝试编译以查看它是否正常工作时我都会收到消息“警告:初始化使指针来自没有演员的整数。“

我已经解决了所有语法错误(我认为),但我只是因为我的生活无法解决这个问题。 我已经完成了所有的演讲幻灯片,但没有一个能够涵盖这一点。

#include  #include  int player1score, player2score; char* answer1, answer11, answer111, answer2, answer22, answer222; int geography() { printf ("The first category is geography. Note: Player 1 always goes first.\n"); char* answer1 = AskForStringAndWait("Player 1: What is the capital of India?"); if (strcmp(answer1,"New Delhi")==0) player1score++; char* answer2 = AskForStringAndWait("Player 2: What is the capital of Iran?"); if (strcmp(answer2,"Tehran")==0) player2score++; char* answer11 = AskForStringAndWait("Player 1: Name a country that borders France that isn't Germany, Italy or Spain."); if (strcmp(answer11,"Luzembourgh")==0 || strcmp(answer11,"Switzerland")==0 || strcmp(answer11,"Belgium")==0) player1score++; char* answer22 = AskForStringAndWait("Player 2: Name one of the main British Channel Islands."); if (strcmp(answer22,"Guernsey")==0 || strcmp(answer22,"Jersey")==0) player2score++; } 

你忘了声明AskForStringAndWait函数。

在现代C(过去的C99)中,这将是一个错误,因为C99不允许调用未声明的函数。

在C89 / 90中,这不是错误。 当调用未声明的函数时,假定它返回一个int值。 所以,你的

 char* answer1 = AskForStringAndWait("Player 1: What is the capital of India?"); 

被解释为尝试使用int值初始化char *指针。 因此警告。

您正在获取整数值并将其转换为指针。 AskForStringAndWait的源代码是AskForStringAndWait ? 我相信你的函数返回一个char,但你将结果存储在一个char指针( char * )中。 此外,您应该注意char answer11char * answer11不相等。 虽然它们具有相同的名称,但是一个是全局变量,另一个是函数内部的局部变量。

 char* answer1, answer11, answer111, answer2, answer22, answer222; 

在这个声明中, answer1char* ,其他只是char类型

但据我所知,你已经再次声明了相同的全局变量

 int geography() {} function 

因此,当您尝试获取这些指针的值时,您可能会收到警告或错误

所以这样宣布

char* answer1, *answer11, *answer111, *answer2, *answer22, *answer222;

内部函数在answer1之前不使用char * ,依此类推

只需使用这些变量

还有返回类型的geography()的问题是int但是你要放入char*