gpt4 book ai didi

c - 为什么我不能使用函数返回值作为参数?

转载 作者:太空宇宙 更新时间:2023-11-04 08:09:52 25 4
gpt4 key购买 nike

这是我遇到问题的代码。

#include <stdio.h>

int getplayerone (void);
int getplayertwo (void);
void output (int getplayerone (), int getplayertwo ());

enum choice
{ r, p, s };
typedef enum choice Choice;

int
main (int argc, char *argv[])
{
//getplayerone();
// getplayertwo();
output (getplayerone (), getplayertwo ());
return 0;
}

int
getplayerone (void)
{
char choice1;
int choice1int;
printf ("Player-1 it is your turn!\n");
printf ("Please enter your choice (p)aper, (r)ock, ir (s)cissors: ");
scanf (" %c", &choice1);
if (choice1 == 'r' || choice1 == 'R')
{
choice1int = 0;
}
else if (choice1 == 'p' || choice1 == 'P')
{
choice1int = 1;
}
else if (choice1 == 's' || choice1 == 'S')
{
choice1int = 2;
}
if (choice1int == 0)
{

}

return choice1int;
}

int
getplayertwo (void)
{
char choice2;
int choice2int;
printf ("\nPlayer-2 it is your turn!\n");
printf ("Please enter your choice (p)aper, (r)ock, ir (s)cissors: ");
scanf (" %c", &choice2);
if (choice2 == 'r' || choice2 == 'R')
{
choice2int = 0;
}
else if (choice2 == 'p' || choice2 == 'P')
{
choice2int = 1;
}
else if (choice2 == 's' || choice2 == 'S')
{
choice2int = 2;
}

return choice2int;
}

void
output (int getplayerone (), int getplayertwo ())
{

Choice p1choice = getplayerone ();
Choice p2choice = getplayertwo ();

if (p1choice == r && p2choice == r)
{
printf ("Draw");
}
else if (p1choice == r && p2choice == p)
{
printf ("Player 2 wins");
}
else if (p1choice == r && p2choice == s)
{
printf ("Player 1 wins");
}
else if (p1choice == s && p2choice == r)
{
printf ("Player 2 wins");
}
else if (p1choice == s && p2choice == p)
{
printf ("Player 1 wins");
}
else if (p1choice == s && p2choice == s)
{
printf ("Draw");
}
else if (p1choice == p && p2choice == r)
{
printf ("Player 1 wins");
}
else if (p1choice == p && p2choice == p)
{
printf ("Draw");
}
else if (p1choice == p && p2choice == s)
{
printf ("Player 2 wins");
}

printf ("%d", p1choice);
}

我需要使用枚举类型来获取每个玩家的输入。这是一款简单的石头剪刀布游戏。我在输出函数类型方面遇到问题,在函数调用中以及在函数体中分配 Choice p1choice 时出现以下错误。

Incompatible integer to pointer conversion passing 'int' to parameter of type 'int (*)()'

Thread 1: EXC_BAD_ACCESS (code=1, address = 0x0)

感谢您的输入和帮助!

最佳答案

你这样调用输出:

output( getplayerone(),  getplayertwo());

用函数本身调用它:

output( getplayerone,  getplayertwo);

关于c - 为什么我不能使用函数返回值作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40251891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com