Warning - Initialization makes pointer from intege

2019-07-07 08:56发布

问题:

I found similar questions but I don't think they applied to my specific problem, so I'm sorry if they do!

I'm learning C as a first year CS student and trying to make a quiz in C, but I can't get anywhere because every time I try to compile to see if it's working I get the message "warning: initialization makes pointer from integer without a cast."

I've worked out all of the syntax errors (I think) but I just for the life of my can't figure this out. I've gone through all of my lecture slides but none of them cover this.

#include <stdio.h>
#include <scc110.h>

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++;
}

回答1:

You forgot to declare AskForStringAndWait function.

In modern C (past-C99) that would be an error, since C99 does not allow calling undeclared functions.

In C89/90 it is not an error. When an undeclared function is called, it is assumed that it returns an int value. So, your

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

is interpreted as an attempt to initialize a char * pointer with an int value. Hence the warning.



回答2:

You are taking an integral value and converting it to a pointer. What is the source code for AskForStringAndWait? I believe your function returns a char, but you are storing that result in a char pointer (char *). Also, you should take note that char answer11 and char * answer11 are not equivalent. Although they have the same name, one is a global variable, and the other is a local variable inside of a function.



回答3:

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

In this declaration answer1 is char* and the others are only char type

But as far as I think you have declared again the same global variables inside the

int geography() {}  function

So when you are trying to get the values of these pointers You might get warning or errors

So declare like this

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

And inside function don't use char * before answer1and so on

just use these variables

Also the problem with the return type of geography() which is int but you are putting into char*