warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’

前端 未结 7 1789
醉话见心
醉话见心 2020-12-09 20:39

So I\'m new to C and am having trouble with whats happening with this warning. What does the warning mean and how can i fix it. The code i wrote is here:

voi         


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 21:17

    The scanf function takes the address of a variable to put the result into.
    Writing scanf("%d", &someVar) will pass the address of the someVar variable (using the & unary operator).
    The scanf function will drop a number into the piece of memory at that address. (which contains your variable)

    When you write scanf("%d", age), you pass the value of the age variable to scanf. It will try to drop a number into the piece of memory at address 0 (since age is 0), and get horribly messed up.

    You need to pass &age to scanf.

    You also need to allocate memory for scanf to read a string into name:

    char name[100];
    scanf("%99s\n", name);
    

提交回复
热议问题