gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:48:30 25 4
gpt4 key购买 nike

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

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/

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