[英]How to use pointers to return multiple values from a function in C
I am currently writing a program for a assigment which requires the use of a function to enable the user to input 3 vairables. 我目前正在编写一个用于分类的程序,该程序需要使用功能来使用户能够输入3个可变项。 I am having difficulty returning these variables to my main function, I have seen other similar questions previously asked and have attempted to use pointers but am unable to get it working.
我很难将这些变量返回到我的主函数中,我曾经遇到过其他类似的问题,并且曾尝试使用指针,但无法使其正常工作。 My attempt is below:
我的尝试如下:
#include <stdio.h>
#include <stdlib.h>
//Function Header for positive values function
double get_positive_value(double* topSpeed, double* year, double*
horsepower);
int main(void){
int reRunProgram = 0;
while (reRunProgram==0)
{
//variable declarations
double tS;
double yR;
double hP;
int menuOption;
int menuOption2;
//menu
printf("1.Create Bugatti\n");
printf("2.Display Bugatti\n");
printf("3.Exit\n");
//user choice
scanf("%d", &menuOption);
//Create car
if (menuOption == 1) {
//run the get positive values function
get_positive_value (&tS, &yR, &hP);
printf("top speed is %lf\n", tS);
}
//Display car (but no car created)
else if (menuOption == 2){
printf("error no car created\n");
}
//Exit
else if (menuOption ==3){
exit(EXIT_FAILURE);
}
}
return 0;
}
double get_positive_value(double* topSpeed, double* year, double*
horsepower)
{
do {
printf("Please enter the top speed of the bugatti in km/h\n");
scanf("%lf", &topSpeed);
} while(*topSpeed<=0);
do{
printf("Please enter the year of the bugatti, in four digit form (e.g. 1999)\n");
scanf("%lf", &year);
} while(*year<=0);
do{
printf("Please enter the horsepower of the bugatti\n");
scanf("%lf", &horsepower);
} while(*horsepower<=0);
}
You can't return multiple values from a function unless you wrap them in a struct
. 除非将它们包装在
struct
否则无法从函数返回多个值。 As far as pointers are concerned you can modify the values that you passed into the function from main. 就指针而言,您可以修改从main传递给函数的值。 I think you're doing it wrong here :
我认为您在这里做错了:
scanf("%lf", &topSpeed);
Since topSpeed
is a pointer to a double and you only need to pass the address of the variable you passed from main (not the address of pointer variable). 由于
topSpeed
是指向double的指针,因此您只需要传递从main传递的变量的地址(而不是指针变量的地址)。 You should instead do: 您应该改为:
do {
printf("Please enter the top speed of the bugatti in km/h\n");
scanf("%lf", topSpeed);
} while(*topSpeed<=0);
do {
printf("Please enter the year of the bugatti, in four digit form (e.g. 1999)\n");
scanf("%lf", year);
} while(*year<=0);
do {
printf("Please enter the horsepower of the bugatti\n");
scanf("%lf", horsepower);
} while(*horsepower<=0);
I hope this helps. 我希望这有帮助。
You declared the variables tS
, yR
& hP
inside the main
function and passed them by reference to the get_positive_value()
function. 你声明的变量
tS
, yR
及hP
内main
功能,并通过参考传递他们get_positive_value()
函数。
So the address of the variables are being passed.
因此,正在传递变量的地址 。 Not the variables themselves.
不是变量本身。
In get_positive_value()
, you are attempting to place some values into the 3 variables using scanf()
where you should've given the address of the variables but gave the address of address instead. 在
get_positive_value()
,您尝试使用scanf()
将一些值放入3个变量中,在此应给定变量的地址,但应给定address的地址 。 &topSpeed
in get_positive_value()
is like &(&tS)
in main()
. get_positive_value()
&topSpeed
get_positive_value()
中的&(&tS)
main()
。
Since you have passed them by reference, in get_positive_value()
, you have the address of tS
, yR
, hP
in topSpeed
, year
, horsepower
respectively. 既然你已经按引用传递它们,
get_positive_value()
你有地址tS
, yR
, hP
在topSpeed
, year
, horsepower
分别。
topSpeed
itself is the address of tS
. topSpeed
本身就是tS
的地址。 Not &topSpeed
. 不是
&topSpeed
。
You should change 你应该改变
scanf("%lf", &topSpeed);
to 至
scanf("%lf", topSpeed);
(likewise for the other 2 variables) (对于其他2个变量同样如此)
Because topSpeed
is having the address of the variable tS
in main()
. 因为
topSpeed
在main()
具有变量tS
的地址。 So if you say &topSpeed
you are trying to access the 'address of address of tS
'. 因此,如果您说
&topSpeed
,则尝试访问' tS
地址的地址'。
When you do *someptr
you are asking for the value, at the memory address this pointer is pointing at. 当您执行
*someptr
您正在询问该指针指向的内存地址的值。
When you do a scanf
and you use &x
for a variable, you do it because you want to store the value at that memory address. 当您执行
scanf
并将&x
用作变量时,您之所以这样做是因为要将值存储在该内存地址中。 So when you do a scanf
with a pointer, you don't use *
because you pass a value instead of an address, to store the value at. 因此,当使用指针执行
scanf
时,不要使用*
因为您传递的是值(而不是地址)来存储值。
You don't use &
either, because you pass the memory address of the pointer instead of the one you actually want to modify. 您也不使用
&
,因为您传递的是指针的内存地址,而不是实际要修改的地址。 This is your main error. 这是您的主要错误。 Lastly, you could
return
those values all at once using a struct
, but pointers are more elegant. 最后,您可以使用
struct
一次return
所有这些值,但是指针更加优雅。
Hope I helped you and I was clear. 希望我能帮到您,我很清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.