Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 格式指定“类型”;int*”;但论点有“类型”;int";_C - Fatal编程技术网

C 格式指定“类型”;int*”;但论点有“类型”;int";

C 格式指定“类型”;int*”;但论点有“类型”;int";,c,C,创建打印体重指数的代码 printf("What is your height in inches?\n"); scanf("%d", height); printf("What is your weight in pounds?\n"); scanf("%d", weight); 我将身高和体重初始化为int height,int weight,但程序不允许我运行它,因为它说两行scanf上的格式都是typeint*。运行此程序时我做错了什么?scanf需要格式(您的%d”)以及变量的内存

创建打印体重指数的代码

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

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

我将身高和体重初始化为
int height
int weight
,但程序不允许我运行它,因为它说两行
scanf
上的格式都是type
int*
。运行此程序时我做错了什么?

scanf
需要格式(您的
%d”
)以及变量的内存地址,它应该将读取的值放在该位置
height
weight
int
,而不是
int
的内存地址(这就是
int*
类型“所说的”:指向
int
的内存地址的指针)。您应该使用操作符
&
将内存地址传递给
scanf

您的代码应该是:

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

printf("What is your weight in pounds?\n");
scanf("%d", &weight);
//For storing value of height
scanf(" %d", &height);
//For storing value of weight
scanf(" %d", &weight);

更新:正如顺磁牛角面包所指出的,引用不是正确的术语。因此我将其更改为内存地址。

scanf
读取标准输入中的字符,根据此处的整数格式说明符解释它们,并将它们存储在相应的参数中

要存储它们,您必须指定
&变量名称
,它将指定存储输入的地址位置

您的
scanf
声明应该是:

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

printf("What is your weight in pounds?\n");
scanf("%d", &weight);
//For storing value of height
scanf(" %d", &height);
//For storing value of weight
scanf(" %d", &weight);

考虑到其他用户所说的,尝试这样的方式

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);

int高度 就像其他人所说的,这是因为当你有一个值是一个原语(比如int),它是用scanf读取的,你必须把内存地址传递给这个原语值。然而,只是为了添加一些尚未提及的内容,字符串的情况并非如此

例1。与原语值一起使用
#包括
int main()
{
int primitiveInteger;//整数(即不允许小数)
scanf(“%i”,&primitiveInteger);//注意操作符的地址通过在变量名前面加上&(address of)操作符来获取primitiveInteger变量的地址。
printf(“您的整数是%i”,primitiveInteger);只需在这里将其打印到标准输出流(很可能是您的终端)
}
例2。不使用原语值
#包括
int main()
{
char*nonPrimitiveString;//初始化一个在C中不是基元的字符串变量
scanf(“%s”,非原语字符串);//注意这里没有运算符的地址
printf(“%s\n”,非基本字符串);//打印字符串
}

什么是“参考”?C中没有引用。这些是指针。@顺磁性牛角面包C确实有引用类型。“指针类型可以从函数类型或对象类型派生,称为引用类型”C11dr§6.2.5 20。也许这个答案在那个上下文中使用了引用。@chux我知道,但是使用它的问题是不幸的和不正确的,因为引用类型与引用不同,更不用说指针了。是的,术语引用是错误的。我换了内存地址。Thanks@The顺磁牛角面包大多同意和更多。在我看来,好的代码检查
scanf()
,好的代码使用
fgets()
并检查/解析结果。健壮的代码使用
fgets()。OP的所有步骤可能都比较多,因此提升了第一步。@chux“健壮代码使用
fgets()
”-完全正确。@ParamagneticCroissant,对不起,
scanf
,如果您检查返回值,就足以扫描整数。