【问题标题】:Format specifies type "int*" but argument has type "int"格式指定类型“int*”但参数类型为“int”
【发布时间】:2014-08-29 19:27:48
【问题描述】:

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

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

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

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

【问题讨论】:

  • @The Paramagnetic Croissant 大多数同意等等。 IMO 好的代码检查 scanf(),更好的代码使用 fgets() 并检查/解析结果。健壮的代码使用fgets() 处理所有这些,并且还处理文件 IO 错误。所有步骤对于 OP 来说可能都很多,所以先推广。
  • @chux "健壮的代码使用fgets()" - 完全正确。
  • @TheParamagneticCroissant,很抱歉,如果您检查返回值,scanf 足以扫描整数。

标签: c


【解决方案1】:

scanf 需要格式(您的"%d")以及变量的内存地址,它应该将读取的值放在其中。 heightweightint,而不是 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);

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

【讨论】:

  • 什么“参考”? C 中没有引用。那些是指针。
  • @The Paramagnetic Croissant C 确实有一个引用类型。 “指针类型可以派生自函数类型或对象类型,称为引用类型”C11dr §6.2.5 20。也许这个答案是在该上下文中使用 reference
  • @chux 我知道这一点,但使用它的问题是不幸和不正确的,因为引用类型与引用不同,更不用说指针了。
  • 是的,术语引用是错误的。我改为内存地址。谢谢
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

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

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

    您的scanf 声明应该是:

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

    【讨论】:

      【解决方案4】:

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

      示例 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.)
      }
      

      示例 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
      }
      

      【讨论】:

        猜你喜欢
        • 2014-04-23
        • 1970-01-01
        • 1970-01-01
        • 2020-08-02
        • 2017-09-04
        • 2015-06-10
        • 1970-01-01
        • 1970-01-01
        • 2016-09-29
        相关资源
        最近更新 更多