gpt4 book ai didi

c - 警告 : format '%d' expects argument of type 'int *' , 但参数 2 的类型为 'int'

转载 作者:太空宇宙 更新时间:2023-11-04 07:56:47 27 4
gpt4 key购买 nike

这是为了我进入 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);
}
}

最佳答案

报错来自你的线路

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

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

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

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

用于输入然后

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

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

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

关于c - 警告 : format '%d' expects argument of type 'int *' , 但参数 2 的类型为 'int',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49502569/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com