C语言 警告:格式“%d”需要类型“int *”,但参数 2 的类型为“int”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4751455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
提问by Zieklecknerizer
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:
所以我是 C 的新手,并且遇到了这个警告发生的问题。警告是什么意思,我该如何解决。我写的代码在这里:
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);
}
回答by SLaks
The scanffunction takes the addressof a variable to put the result into.
Writing scanf("%d", &someVar)will pass the addressof the someVarvariable (using the &unary operator).
The scanffunction will drop a number into the piece of memory at that address. (which contains your variable)
该scanf函数获取变量的地址以将结果放入。
写入将通过地址的的变量(使用一元运算符)。
该函数会将一个数字放入该地址的内存中。(其中包含您的变量)scanf("%d", &someVar)someVar&scanf
When you write scanf("%d", age), you pass the valueof the agevariable to scanf. It will try to drop a number into the piece of memory at address 0(since ageis 0), and get horribly messed up.
编写时scanf("%d", age),将变量的值传递age给scanf。它会尝试将一个数字放入地址处的内存块中0(因为age是0),并且会变得非常混乱。
You need to pass &ageto scanf.
你需要传递&age给scanf.
You also need to allocate memory for scanfto read a string into name:
您还需要分配内存scanf以将字符串读入name:
char name[100];
scanf("%99s\n", name);
回答by Zieklecknerizer
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 需要指针参数 - 这是函数可以在 C 中修改参数的唯一方法。为了解决这个问题,您需要:
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).
它传递 addressof age,它现在是一个指针(指针是一个包含指向另一个内存区域的地址的变量)。
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).
哎哟,请不要!好吧,我需要解释一下。您已经要求一个指向字符类型的指针,但就每个人而言,您所得到的只是一个字符,而不是整个字符串。没错,空间不够。为什么 C 让你这样做?嗯,C 基本上是可移植的汇编程序,因此您可以在内存中随心所欲地编写代码(或者更确切地说,您可以尝试编译器不会不同意,但操作系统可以并且可能会)。
What you need to do is read up on memory allocation using mallocin order to allocate some space, otherwise I can input a massive string and it gets put at the address of name onwards.
您需要做的是读取内存分配使用malloc以分配一些空间,否则我可以输入一个巨大的字符串并将其放在名称的地址上。
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.
尽管绝不会,这可能会导致基于堆栈的漏洞。什么,堆栈?是的。堆栈是一种 FILO 结构,每次调用函数时,都会在堆栈上添加空间用于返回地址和函数参数,以及频繁的函数作用域变量。
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.
请参阅此问题:C 中的字符串读取。
回答by ChrisJ
You should write scanf("%d", &age), as the scanffunction needs to modifythe value of age, hence the need to pass it "by address" and not "by value".
您应该编写scanf("%d", &age),因为该scanf函数需要修改的值age,因此需要“按地址”而不是“按值”传递它。
回答by MTilsted
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.
这意味着它期望一个“int *”(即:指向整数的指针),但你给它“int”,它是一个整数。要修复它,在 scanf 行中添加 & 到 age 使其成为。
scanf("%d\n", age);
scanf("%d\n", 年龄);
回答by rerun
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.
我假设它抱怨该行scanf("%d\n", age);问题是 scan f 期望指向您的变量而不是变量的指针。您需要通过附加“&”来获取变量的地址,您应该没问题。
回答by David Thornley
The warning means exactly what it says: the compiler expects a pointer to intrather than an intin the scanfcall.
该报警装置正是它说:编译器期望一个指针int,而不是int在scanf通话。
Quick fix: replace agewith &age, and everything will work.
快速修复:替换age为&age,一切都会正常。
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.
C 按值传递所有参数,这意味着如果传递变量,则仅传递变量的值。接收函数可以随心所欲地修改该值,但不会更改原始值。要更改变量,您需要以某种方式传递一个指向该变量的值,在 C 中它是一个指针。
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 scanfto change the value of age, so you need to pass it a pointer.
在这种情况下,您想要scanf更改 的值age,因此您需要向它传递一个指针。
回答by bobbogo
char* name = "";
namepoints at a bit or memory large enough to hold a single null character.
name指向足以容纳单个空字符的位或内存。
scanf("%s\n", name);
You ask for a unknown number of characters to be read and stored at this address. Anythingcould happen. You mustensure namehass the address of a chunk of memory large enough to hold anythingscanf()might read.
您要求在此地址读取和存储未知数量的字符。任何事情都可能发生。您必须确保name拥有足够大的内存块的地址,以容纳可能读取的任何scanf()内容。
char twenty_bytes[20] = "";
scanf("%s\n", twenty_bytes);

