So I have two functions, one function is passing by reference a variable and the other one is returning the result.
所以我有两个函数,一个函数通过引用传递一个变量,另一个函数返回结果。
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;
}
I'm having a hard time trying to figure out why it gives me this warning in the terminal:
我很难弄清楚为什么它会在终端给我这个警告:
main.c:38:36: warning: format specifies type 'int' but the argument has type
'int *' [-Wformat]
printf("Total miles saved: %d\n", totalMiles);
~~ ^~~~~~~~~~
You are probably wondering why the dailyMiles
function's datatype is void
; well, I'm calling other functions that ask for users input so whenever I call it in my main, it asks for users input two times.
您可能想知道为什么dailyMiles函数的数据类型为void;好吧,我正在调用其他要求用户输入的函数,所以无论何时我在main中调用它,它都要求用户输入两次。
4 个解决方案
#1
2
You don't want to print the pointer itself (totalMiles
), you want to print what it points to (*totalMiles
).
您不想打印指针本身(totalMiles),您想要打印它指向的内容(* totalMiles)。
printf("Total miles saved: %d\n", *totalMiles);
#2
2
In function void dailyMiles(int *totalMiles)
at statement 10: printf("Total miles saved: %d\n", **totalMiles**);
在函数void dailyMiles(int * totalMiles)at语句10:printf(“保存的总里程数:%d \ n”,** totalMiles **);
Instead of using totalMiles
You should use *totalMiles
because you created an integer type pointer. To access the data inside it you will have to use the asterisk operator that is *
而不是使用totalMiles您应该使用* totalMiles,因为您创建了一个整数类型指针。要访问其中的数据,您必须使用星号运算符*
For example:
例如:
int *totalMiles;
...
...
...
printf("Total miles saved: %d\n", *totalMiles);
#3
1
In the function totalMiles dailyMiles (int *totalMiles), totalMiles is a pointer to an integer. So, to print its values just add * in front of totalMiles as given below:
在函数totalMiles dailyMiles(int * totalMiles)中,totalMiles是一个指向整数的指针。因此,要打印其值,只需在totalMiles前添加*,如下所示:
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;
}
}
Hope this helps you. If it isn't working kindly let me know.
希望这对你有所帮助。如果它不工作,请告诉我。
#4
0
You need to "dereference" your int*
(pointer) to get an int
.
你需要“取消引用”你的int *(指针)才能得到一个int。
#1
2
You don't want to print the pointer itself (totalMiles
), you want to print what it points to (*totalMiles
).
您不想打印指针本身(totalMiles),您想要打印它指向的内容(* totalMiles)。
printf("Total miles saved: %d\n", *totalMiles);
#2
2
In function void dailyMiles(int *totalMiles)
at statement 10: printf("Total miles saved: %d\n", **totalMiles**);
在函数void dailyMiles(int * totalMiles)at语句10:printf(“保存的总里程数:%d \ n”,** totalMiles **);
Instead of using totalMiles
You should use *totalMiles
because you created an integer type pointer. To access the data inside it you will have to use the asterisk operator that is *
而不是使用totalMiles您应该使用* totalMiles,因为您创建了一个整数类型指针。要访问其中的数据,您必须使用星号运算符*
For example:
例如:
int *totalMiles;
...
...
...
printf("Total miles saved: %d\n", *totalMiles);
#3
1
In the function totalMiles dailyMiles (int *totalMiles), totalMiles is a pointer to an integer. So, to print its values just add * in front of totalMiles as given below:
在函数totalMiles dailyMiles(int * totalMiles)中,totalMiles是一个指向整数的指针。因此,要打印其值,只需在totalMiles前添加*,如下所示:
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;
}
}
Hope this helps you. If it isn't working kindly let me know.
希望这对你有所帮助。如果它不工作,请告诉我。
#4
0
You need to "dereference" your int*
(pointer) to get an int
.
你需要“取消引用”你的int *(指针)才能得到一个int。