这是我的into to 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.temperature
保存一个float,因此您宁愿使用另一个int变量,例如
int itemp;
scanf ("%d", &itemp);

对于输入,然后
ph[i].vi.temperature = (float) itemp;

为了演员。
或者,你可以简单地scanf ("%f", &ph[i].vi.temperature);然后保留完整的部分。
我不知道你的需求,也不知道你代码背后的逻辑。
注意:我不确定您是否以符合您需要的方式使用了返回值scanf
在您的情况下,scanf可以返回01EOF

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

10-11 22:07
查看更多