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

标签 c function enums parameters

这是我遇到问题的代码。

#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/

相关文章:

c - C 字节读取器中的段错误(核心转储)

function - 如何编写一个只接受一个枚举变体作为输入的函数?

java - 将频率转换为 java.time.Period 类型

c - qsort() 问题 - 排序不正确 (C)

c - 如何测试 strdup() 是否可用?

C 嵌套结构语法之争

C++::成员函数数组

python - 如何从Python中的单个input()函数获取多个输入?

c# - 最佳实践 : use enum or not to store dropdown values

当存在多个类时,Java 中 ArrayList 出现问题并帮助修复代码