【问题标题】:warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int'警告:格式“%d”需要“int *”类型的参数,但参数 2 的类型为“int”
【发布时间】:2018-03-27 00:18:48
【问题描述】:

这是我的 C 类课程,我不知道为什么会出现此错误:

while (scanf ("%d", (int)ph[i].vi.temperature) < 0) { 
warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int'

代码:

struct vitalInformation {
    float temperature;
    unsigned int systolicPressure;
    unsigned int diastolicPressure;
};

struct activityInformation {
    unsigned int stepCount;
    unsigned int sleepHours;
};

union patientHealth{
    struct vitalInformation vi;
    struct activityInformation ai;
} ph[100]; 

int i = 0;

int menu(){
    int option;

    printf ("Please enter the number for the desired action (1, 2, 3):\n");
    printf ("1 - Enter some patient vital information\n");
    printf ("2 - Enter some patient activity information\n");
    printf ("3 - Print summary information on the patient information and exit the program\n");

    scanf ("%d", &option);

    while (scanf("%d", &option) || option<1 || option>3) {
        printf ("Please enter 1, 2, or 3\n\n");
        printf ("Please enter the number for the desired action (1, 2, 3):\n");
        printf ("1 - Enter some patient vital information\n");
        printf ("2 - Enter some patient activity information\n)");
        printf ("3 - Print summary information on the patient information and exit the program\n");

        fflush (stdin);
        scanf ("%d", &option);

    }

    return option;
}

void patientVitalInfo(int *countP, float *minT, float *maxT, int *minS, int *maxS, int *minD, int *maxD) {
    printf ("Enter the temperature: ");
    scanf ("%f", &ph[i].vi.temperature);

    while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {
        printf ("Please enter an integral unsigned number\n");
        printf ("Enter the temperature: ");

        fflush (stdin);
        scanf ("%f", &ph[i].vi.temperature);
    }
}

【问题讨论】:

  • scanf 需要指针作为参数,因为它正在存储值。您没有传递与错误消息完全相同的指针。您似乎已经正确完成了两次,一次错误,但我不确定您为什么要读取至少 2 个值。似乎是复制/粘贴和逻辑错误。
  • 请注意ph[i].vi.temperaturefloat
  • 代码后面有scanf ("%f", &amp;ph[i].vi.temperature);,为什么不做同样的事情而不是scanf ("%d", (int)ph[i].vi.temperature)
  • @DavidC.Rankin:fflush 对于输入流的行为是为许多在这里提问的人定义的。它不是由 C 标准定义的,而是 defined by Microsoft:“如果流对输入打开,则 fflush 会清除缓冲区的内容。”
  • @DavidC.Rankin:我不是在告诉人们使用它,也不建议这样做。我认识到使用它的初学者有这样做的理由,并建议他们从代码中删除它会在他们所处的情况下导致问题。使用非可移植行为有的原因.当有人致电pthread_create 时,您是否警告这是不可移植的行为,所以他们应该删除它?当有人致电CreateWindow 时,您是否警告这是不可移植的行为? fflush(stdin) 与那些没有什么不同。它是 C 标准欢迎的扩展。

标签: c


【解决方案1】:

报告的错误来自你的行

while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {

这会将您从ph[i].vi.temperature(在本例中为float)中获得的任何内容转换为int,而scanf 需要指向int 的指针。

现在,在您的情况下,您似乎需要温度为 int,而 ph[i].vi.temperature 持有一个float,所以你宁愿使用另一个int变量,比如说

int itemp;
scanf ("%d", &itemp);

输入然后

ph[i].vi.temperature = (float) itemp;

用于选角。 或者,您可以简单地 scanf ("%f", &amp;ph[i].vi.temperature); 然后保留不可分割的部分。 我不知道您的需求,也不知道您的代码背后的逻辑。

注意:我不确定您是否以符合您需要的方式使用 scanf 的返回值。 在您的情况下,scanf 可以返回 01EOF

【讨论】:

  • float 读取为float,或将类型更改为int。为什么要通过一个临时变量然后转换为float
  • @DavidC.Rankin - 用户希望(出于某种未知原因)将 int 存储在浮点数中......
猜你喜欢
  • 2011-06-12
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 2013-04-22
  • 2021-06-22
相关资源
最近更新 更多