简体   繁体   中英

How do I return a sorted struct to my main function?

Goal: Sort a Struct of 3 Dice and Return Sorted

#include <stdio.h>
#include <string.h>
#include <time.h>  

Struct

struct RolledDice
{
    int die1 ;
    int die2 ;
    int die3 ;
} dice;

Prototype

void sort_dice(struct RolledDice dice );

Main

int main() {
srand ( time(NULL) );

dice.die1 = rand() % 6 + 1 ;
dice.die2 = rand() % 6 + 1 ;
dice.die3 = rand() % 6 + 1 ;

sort_dice(dice);

return 0;
}

Sort Dice Function

void sort_dice(struct RolledDice dice) {

    printf("Die 1: %d \n", dice.die1);
    printf("Die 2: %d \n", dice.die2);
    printf("Die 3: %d \n\n", dice.die3);

    int tempDie = 0;

(There may be a better way to do this......but this is the best I could come up with)

    while ( dice.die1 < dice.die2 || dice.die1 < dice.die3 || dice.die2 < dice.die3 )
    {
        if ( dice.die1 < dice.die2 )
         {
         tempDie = dice.die1 ;
         dice.die1 = dice.die2 ;
         dice.die2 = tempDie ;
         }
        if ( dice.die1 < dice.die3 )
         {
         tempDie = dice.die1 ;
         dice.die1 = dice.die3 ;
         dice.die3 = tempDie ;
         }
        if (dice.die2 < dice.die3 )
         {
         tempDie = dice.die2 ;
         dice.die2 = dice.die3 ;
         dice.die3 = tempDie ;
         }
    }

    printf( "Die 1: %d \n", dice.die1 );
    printf( "Die 2: %d \n", dice.die2 );
    printf( "Die 3: %d \n\n", dice.die3 );
}

I tried changing the void to int and struct but kept getting errors, or it wouldn't update the struct in the main .

You're sorting a copy of your dice structure, which is lost as soon as it goes out of scope when the function returns.

change your function to:

struct RolledDice sort_dice(struct RolledDice dice) {

and in the end just return dice

usage:

int main() {
srand ( time(NULL) );

dice.die1 = rand() % 6 + 1 ;
dice.die2 = rand() % 6 + 1 ;
dice.die3 = rand() % 6 + 1 ;

dice = sort_dice(dice);

// print the sorted struct here
printf( "Die 1: %d \n", dice.die1 );
printf( "Die 2: %d \n", dice.die2 );
printf( "Die 3: %d \n\n", dice.die3 );  
return 0;
}

Or pass dice as a pointer and use dice-> instead of dice. in your function (heavier refactoring but less memory copy and thus more performant)

void sort_dice(struct RolledDice *dice) {
...
 dice->die1 = dice->die3 ;
...

usage:

int main() {
srand ( time(NULL) );

dice.die1 = rand() % 6 + 1 ;
dice.die2 = rand() % 6 + 1 ;
dice.die3 = rand() % 6 + 1 ;

sort_dice(&dice);  // pass as pointer so it can be modified in the function

// print the sorted struct here
printf( "Die 1: %d \n", dice.die1 );
printf( "Die 2: %d \n", dice.die2 );
printf( "Die 3: %d \n\n", dice.die3 );
return 0;
}

note: your original code prints the bubble-sorted values correctly within your sort routine. I suppose that the problem is that you did not find a way to update it in the caller function (creating a function to just print sorted values is useless)

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