c - printf 在调用通过引用传递变量的函数时发出警告

标签 c

所以我有两个函数,一个函数通过引用传递一个变量,另一个函数返回结果。

void dailyMiles(int *totalMiles)
{

    int milesDriven, totalDays, totalPeople;

    peopleInVehicle(&totalPeople); //calling other function
    daysPerWeek(&totalDays); // calling other function

    printf("Enter the amount of miles driven per day: \n");
    scanf("%d", &milesDriven);


    *totalMiles = (milesDriven * totalDays * 52 / totalPeople) * 2;

    printf("Total miles saved: %d\n", totalMiles);

    return;
}

int outputMiles()
{
    int totalMiles;

    dailyMiles(&totalMiles);

    return totalMiles;
}

我很难弄清楚为什么它会在终端中给我这个警告:

main.c:38:36: warning: format specifies type 'int' but the argument has type
  'int *' [-Wformat]
    printf("Total miles saved: %d\n", totalMiles);
                               ~~     ^~~~~~~~~~

您可能想知道为什么 dailyMiles 函数的数据类型是 无效;好吧,我正在调用其他要求用户输入的函数 每当我在主程序中调用它时,它都会要求用户输入两次。

最佳答案

您不想打印指针本身 (totalMiles),您想要打印它指向的内容 (*totalMiles)。

printf("Total miles saved: %d\n", *totalMiles);

关于c - printf 在调用通过引用传递变量的函数时发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52232318/

相关文章:

c - 使用带有指针的结构读取二进制文件中的某个位置

使用 OpenMP 计算直方图

c - 数组 a[i] 按降序打印,但我的逻辑是升序

c - 函数声明有多重要?

c - 两个c文件之间的斐波那契共享内存进程

c - 使用标题的标题而不是重复

c - 读取文件 c 的链表问题

c++ - 取消引用和引用类示例

c - 如何重新启动C语言程序?

c++ - 为什么 RGBTRIPLE 和 RGBQUAD 按 BGR 顺序存储数据?