C语言 格式指定类型“int*”但参数类型为“int”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25574981/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:19:18  来源:igfitidea点击:

Format specifies type "int*" but argument has type "int"

c

提问by Josh Fair

Creating a code that prints out Body Mass Index

创建一个打印出身体质量指数的代码

printf("What is your height in inches?\n");
scanf("%d", height);

printf("What is your weight in pounds?\n");
scanf("%d", weight);

I have height and weight as initialized as int height, int weight, but the program is not letting me run it because it says the format is type int*on both scanflines. What am I doing wrong to get this program to run?

我的身高和体重初始化为int height, int weight,但程序不允许我运行它,因为它说格式int*在两scanf行都是类型。让这个程序运行我做错了什么?

回答by braindf

scanfrequires the format (your "%d") and also a memory address of the variable where it should put the value that was read. heightand weightare int, not the memory address of an int(this is what int *type 'says': a pointer to a memory address of an int). You should use the operator &to pass the memory address to scanf.

scanf需要格式(您的"%d")以及变量的内存地址,它应该放置读取的值。height并且weightint,而不是 an 的内存地址int(这是int *“说”的类型:指向 an 的内存地址的指针int)。您应该使用运算符&将内存地址传递给scanf.

Your code should be:

你的代码应该是:

printf("What is your height in inches?\n");
scanf("%d", &height);

printf("What is your weight in pounds?\n");
scanf("%d", &weight);

Update: As The Paramagnetic Croissant pointed out, reference is not the correct term.So I changed it to memory address.

更新:正如顺磁羊角面包指出的那样,引用不是正确的术语。所以我把它改成了内存地址。

回答by Xavier Dass

taking into account what the other users have said, try something like this;

考虑到其他用户所说的,尝试这样的事情;

int height;                  <---- declaring your variables
int weight;
float bmi;                   <---- bmi is a float because it has decimal values

printf("What is your height in inches?\n");
scanf("%d", &height);           <----- don't forget to have '&' before variable you are storing the value in

printf("What is your weight in pounds?\n");
scanf("%d", &weight);


bmi = (weight / pow(height, 2)) * 703;    <---- the math for calculating BMI
printf("The BMI is %f\n", bmi);

(for this you will need to include the math.h library.)

(为此,您需要包含 math.h 库。)

回答by brff19

Like others have said, it's because when you have a value that is a primitive (like an int) that is being read with scanf you have to pass the memory address to that primitive value. However, just to add something that has not been mentioned yet, the same is not true for strings.

就像其他人所说的那样,这是因为当您有一个使用 scanf 读取的原始值(如 int)时,您必须将内存地址传递给该原始值。但是,只是添加一些尚未提及的内容,对于字符串而言并非如此。

Example 1. Using with a primitive value

示例 1. 使用原始值

#include <stdio.h>

int main()
{
    int primitiveInteger; // integer (i.e. no decimals allowed)
    scanf("%i", &primitiveInteger); // notice the address of operator getting the address of the primitiveInteger variable by prepending the variable name with the & (address of) operator.
    printf("Your integer is %i", primitiveInteger); simply printing it out here to the stdout stream (most likely your terminal.)
}

Example 2. Not using a primitive value

示例 2. 不使用原始值

#include <stdio.h>

int main()
{
    char *nonPrimitiveString; // initialize a string variable which is NOT a primitive in C
    scanf("%s", nonPrimitiveString); // notice there is no address of operator here
    printf("%s\n", nonPrimitiveString); // prints string
}

回答by user1336087

scanfreads characters from the standard input, interprets them according to the format specifiers here "%d"for integers and stores them in corresponding arguments.

scanf从标准输入读取字符,根据这里"%d"的整数格式说明符解释它们,并将它们存储在相应的参数中。

To store them you must specify &variable_name, it will specify the address location where the input should be stored.

要存储它们,您必须指定&variable_name,它将指定应存储输入的地址位置。

Your scanfstatement should be:

你的scanf陈述应该是:

//For storing value of height
scanf(" %d", &height);
//For storing value of weight
scanf(" %d", &weight);