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

前端 未结 7 1752
醉话见心
醉话见心 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 20:58

    You should write scanf("%d", &age), as the scanf function needs to modify the value of age, hence the need to pass it "by address" and not "by value".

    0 讨论(0)
  • 2020-12-09 21:00
    char* name = "";
    

    name points at a bit or memory large enough to hold a single null character.

    scanf("%s\n", name);
    

    You ask for a unknown number of characters to be read and stored at this address. Anything could happen. You must ensure name hass the address of a chunk of memory large enough to hold anything scanf() might read.

    char twenty_bytes[20] = "";
    scanf("%s\n", twenty_bytes);
    
    0 讨论(0)
  • 2020-12-09 21:01

    The problem is this line:

    scanf("%d\n", age);
    

    scanf expects pointer arguments - this is the only way functions can modify parameters in C. In order to fix this one, you need to:

    scanf("%d\n", &age);
    

    Which passes the addressof age, which is now a pointer (a pointer is a variable containing an address to another area of memory).

    As for this:

    char* name = "";
    

    Ouch-ow-please-don't! Ok, I need to explain. You've asked for a pointer to a character type, but as far as everyone's concerned, all you've got is a character, not a whole string. That's right, not enough space. Why does C let you do this? Well, C is basically portable assembler, so you can write wherever you like in memory (or rather, you can try and the compiler won't disagree, but the operating system can and probably will).

    What you need to do is read up on memory allocation using malloc in order to allocate some space, otherwise I can input a massive string and it gets put at the address of name onwards.

    This can, although by no means will, lead to stack-based vulnerabilities. What, stacks? Yes. A stack is a FILO structure and every time you call a function, you add space onto the stack for your return address and function arguments, as well as frequently function-scope variables.

    Where this becomes a problem is if you don't check input sizes. Then, I can write massive values to your stack, including executable code... see Buffer Overflow.

    So how do I mitigate this, I hear you say, just yearning not to implement software vulnerabilities? You use functions that allow you to specify your buffer input size and read into a buffer of a specific size, and go from there. That's it. That simple.

    See this question: string reading in C.

    0 讨论(0)
  • 2020-12-09 21:11

    I'm assuming its complaining about the line scanf("%d\n", age); The issue is that scan f expects a pointer to your variable not the variable. You need to get an address to variable by perpending a '&` and you should be fine.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-09 21:18

    The warning means exactly what it says: the compiler expects a pointer to int rather than an int in the scanf call.

    Quick fix: replace age with &age, and everything will work.

    C passes all arguments by value, meaning that if you pass a variable only the value of the variable is passed. The receiving function can modify that value all it wants, but the original value isn't changed. To change the variable, you need to pass a value that points to the variable somehow, which in C is a pointer.

    To get a pointer to a variable, prefix it with &. To use a variable you've got a pointer to, prefix the pointer value with *.

    In this case, you want scanf to change the value of age, so you need to pass it a pointer.

    0 讨论(0)
提交回复
热议问题