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:
void main(void)
{
char* name = "";
int age = 0;
开发者_运维百科 printf("input your name\n");
scanf("%s\n", name);
printf("input your age\n");
scanf("%d\n", age);
printf("%s %d\n", name, age);
}
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);
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.
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".
It mean that it expect a "int *" (That is: A pointer to an integer) but you give it "int" which is an integer. To fix it add & to age in the scanf line so it become.
scanf("%d\n", age);
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.
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.
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);
hey friend recently means today I have also gone through this stuff.don't worry it's simple.
problem in the below lines of code :
scanf("%s\n", name);
and
scanf("%d\n", age);
you simply written these lines like printf() function. if you are using the scanf() function then always use ampersand(&) before the variable in the scanf() function because it's necessary. & is the 'Address of ' operator, it gives the location of the number(address) used by the variable in memory.
your corrected lines of code:
scanf("%s\n", &name);
and
scanf("%d\n", &age);
Next time when you are going to writting this type of code or using scanf() function always try to keep in mind this stuff due to that you will not get the problem in your code.
Thank you for asking the question due that I also will not make such a mistake in the future while writing such type of code.
精彩评论