简体   繁体   中英

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. 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 . As far as pointers are concerned you can modify the values that you passed into the function from 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). 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.

So the 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. &topSpeed in get_positive_value() is like &(&tS) in 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.

topSpeed itself is the address of tS . Not &topSpeed .

You should change
scanf("%lf", &topSpeed);
to
scanf("%lf", topSpeed);
(likewise for the other 2 variables)

Because topSpeed is having the address of the variable tS in main() . So if you say &topSpeed you are trying to access the 'address of address of tS '.

When you do *someptr you are asking for the value, at the memory address this pointer is pointing at.

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. 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.

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.

Hope I helped you and I was clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM