如何使用指针从C中的函数返回多个值

如何使用指针从C中的函数返回多个值,c,C,我目前正在为一个需要使用一个函数才能让用户输入3个值的分配编写一个程序。我很难将这些变量返回到我的主函数中,我看到了之前提出的其他类似问题,并尝试使用指针,但无法使其工作。我的尝试如下: #include <stdio.h> #include <stdlib.h> //Function Header for positive values function double get_positive_value(double* topSpeed, double* year,

我目前正在为一个需要使用一个函数才能让用户输入3个值的分配编写一个程序。我很难将这些变量返回到我的主函数中,我看到了之前提出的其他类似问题,并尝试使用指针,但无法使其工作。我的尝试如下:

#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);
}
#包括
#包括
//正值函数的函数头
双倍获得正值(双倍*最高速度、双倍*年份、双倍*
马力);
内部主(空){
int-reRunProgram=0;
while(重新设置程序==0)
{
//变量声明
双T;
两年;
双hP;
int菜单选项;
int菜单选项2;
//菜单
printf(“1.Create Bugatti\n”);
printf(“2.Display Bugatti\n”);
printf(“3.退出\n”);
//用户选择
scanf(“%d”,菜单选项(&M);
//创造汽车
如果(菜单选项==1){
//运行“获取正值”函数
获得正值(&tS、&yR和&hP);
printf(“最高速度为%lf\n”,tS);
}
//显示汽车(但未创建汽车)
else if(菜单选项==2){
printf(“没有创建汽车的错误\n”);
}
//出口
else if(菜单选项==3){
退出(退出失败);
}
}   
返回0;
}
双倍获得正值(双倍*最高速度、双倍*年份、双倍*
马力)
{
做{
printf(“请输入布加迪的最高速度,单位为km/h\n”);
扫描速度(“%lf”、&topSpeed);

}while(*topSpeed除非将多个值包装到
结构中,否则无法从函数返回多个值。就指针而言,可以修改从main传递到函数中的值。我认为您在这里做得不对:

scanf("%lf", &topSpeed);
由于
topSpeed
是指向双精度的指针,您只需要传递从main传递的变量的地址(而不是指针变量的地址)。您应该改为:

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);
do{
printf(“请输入布加迪的最高速度,单位为km/h\n”);
扫描速度(“%lf”,最高速度);

}while(*topSpeed除非将多个值包装到
结构中,否则无法从函数返回多个值。就指针而言,可以修改从main传递到函数中的值。我认为您在这里做得不对:

scanf("%lf", &topSpeed);
由于
topSpeed
是指向双精度的指针,您只需要传递从main传递的变量的地址(而不是指针变量的地址)。您应该改为:

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);
do{
printf(“请输入布加迪的最高速度,单位为km/h\n”);
扫描速度(“%lf”,最高速度);

}而(*topSpeed您在
main
函数中声明了变量
tS
yR
hP
,并通过引用
get_positive_value()
函数将其传递

所以传递的是变量的地址,而不是变量本身

get\u positive\u value()
中,您试图使用
scanf()
将一些值放入3个变量中,您本应给出变量的地址,但却给出了地址的地址。
get\u positive\u value()
中的
&topSpeed
类似于
main()
中的
&tS)

由于您通过引用传递了它们,因此在
get_positive_value()
中,
tS
yR
hP
分别位于
topSpeed
year
horspower

topSpeed
本身是
tS
的地址。而不是
&topSpeed

您应该更改
scanf(“%lf”、&topSpeed);


scanf(“%lf”,最高速度);

(其他2个变量也是如此)

因为
topSpeed
main()
中有变量
tS
的地址。因此,如果您说
&topSpeed
,您正试图访问“tS
的地址”。

您在
main
函数中声明了变量
tS
yR
hP
,并通过引用
get\u positive\u value()
函数将其传递

所以传递的是变量的地址,而不是变量本身

get\u positive\u value()
中,您试图使用
scanf()
将一些值放入3个变量中,您本应给出变量的地址,但却给出了地址的地址。
get\u positive\u value()
中的
&topSpeed
类似于
main()
中的
&tS)

由于您通过引用传递了它们,因此在
get_positive_value()
中,
tS
yR
hP
分别位于
topSpeed
year
horspower

topSpeed
本身是
tS
的地址。而不是
&topSpeed

您应该更改
scanf(“%lf”、&topSpeed);


scanf(“%lf”,最高速度);

(其他2个变量也是如此)

因为
topSpeed
main()
中有变量
tS
的地址。因此,如果您说
&topSpeed
,您正试图访问“tS
的地址”。
当你这样做的时候
*someptr
您正在请求该指针指向的内存地址处的值

当您对变量执行
scanf
并使用
&x
时,您这样做是因为您希望将值存储在该内存地址处。因此,当您使用指针执行
scanf
时,您不会使用
*
,因为您传递的是一个值而不是一个地址来存储该值

您也不使用
&
,因为您传递内存