C 如何将排序结构返回到主函数?

C 如何将排序结构返回到主函数?,c,function,struct,return,C,Function,Struct,Return,目标:对一个由3个骰子组成的结构进行排序,并返回排序后的 #include <stdio.h> #include <string.h> #include <time.h> 原型 void sort_dice(struct RolledDice dice ); 主要 排序骰子函数 void sort_dice(struct RolledDice dice) { printf("Die 1: %d \n", dice.die1); pri

目标:对一个由3个骰子组成的结构进行排序,并返回排序后的

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

void sort_dice(struct RolledDice dice );
主要

排序骰子函数

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;
(也许有更好的办法……但这是我能想到的最好办法)

while(dice.die1

我尝试将
void
更改为
int
struct
,但不断出现错误,或者它不会更新
中的
struct
,您正在排序
骰子
结构的副本,当函数返回时,它一旦超出范围就会丢失

将您的功能更改为:

struct RolledDice sort_dice(struct RolledDice dice) {
最后只需
返回骰子

用法:

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;
}
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;
}
或者将
dice
作为指针传递,并在函数中使用
dice->
而不是
dice.
(更重的重构但更少的内存拷贝,因此性能更高)

用法:

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;
}
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;
}

注意:原始代码在排序例程中正确打印气泡排序值。我认为问题在于,您没有找到在调用函数中更新它的方法(创建一个只打印排序值的函数是无用的)

您正在排序
骰子
结构的副本,该结构在函数返回时一旦超出范围就会丢失

将您的功能更改为:

struct RolledDice sort_dice(struct RolledDice dice) {
最后只需
返回骰子

用法:

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;
}
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;
}
或者将
dice
作为指针传递,并在函数中使用
dice->
而不是
dice.
(更重的重构但更少的内存拷贝,因此性能更高)

用法:

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;
}
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;
}

注意:原始代码在排序例程中正确打印气泡排序值。我想问题在于您没有找到在调用函数中更新它的方法(创建一个只打印排序值的函数是没有用的)

我已经尝试了您建议的第一个选项,但当我回到main时,仍然会丢失排序版本。我需要将这些骰子传递给另一个函数来确定排名(这要求它们从大到小排序)。尝试第二个版本需要更长的时间…我将代码更新为第二个版本。它工作得很好。非常感谢你的帮助。你太棒了:)另外,谢谢你让你的回复简单到我能理解的程度……非常感谢!谢谢顺便说一句,我刚刚测试了第一个版本,它可以工作,但调用时确实必须分配
dice
,否则它不工作
dice=sort_dice(dice)。也许你忽略了。无论如何,第二个版本更有效,所以是个不错的选择。我已经尝试了你建议的第一个选项,但是当我回到主版本时,我仍然丢失了排序后的版本。我需要将这些骰子传递给另一个函数来确定排名(这要求它们从大到小排序)。尝试第二个版本需要更长的时间…我将代码更新为第二个版本。它工作得很好。非常感谢你的帮助。你太棒了:)另外,谢谢你让你的回复简单到我能理解的程度……非常感谢!谢谢顺便说一句,我刚刚测试了第一个版本,它可以工作,但调用时确实必须分配
dice
,否则它不工作
dice=sort_dice(dice)。也许你忽略了。无论如何,第二个版本更有效,所以是个不错的选择。