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

时间:2022-09-06 07:37:38

Here is the code that I am having trouble with.

这是我遇到问题的代码。

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

I am required to use an enumerated type to get the input of each player. This is a simple rock, paper scissors game. I am having trouble with my output function types and I am getting the following errors in the function call, as well as when I assign Choice p1choice in the function body.

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

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

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

Thank you for the input and help!

感谢您的输入和帮助!

3 个解决方案

#1


1  

you call output this way:

你用这种方式调用输出:

output( getplayerone(),  getplayertwo());

call it with the functions itself:

用函数本身调用它:

output( getplayerone,  getplayertwo);

#2


1  

Why can't I use a function return value as a parameter?

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

You can. This is the proper call:

您可以。这是正确的电话:

output( getplayerone(),  getplayertwo());

If

如果

void output(int r1,int r2); // also `void output(int, int);`  would do
void output(int r1,int r2){
//...
}

Since you declared your function output as:

因为您将函数输出声明为:

void output(int getplayerone(),int getplayertwo());
void output(int getplayerone(),int getplayertwo()){
//...
}

You need to pass function pointers as parameters:

您需要将函数指针作为参数传递:

   output(getplayerone, getplayertwo);

The small example program:

小例子程序:

#include <stdio.h>

int getplayerone (void);
int getplayertwo (void);

void output (int ret_getplayerone, int ret_getplayertwo);
void output1 (int (*f1)(), int (*f2)() );

int main (int argc, char *argv[])
{
    int r1, r2;
    // 1.
    output (r1=getplayerone(), r2=getplayertwo());// OK, but there is no need to do so 
    output ( getplayerone(),   getplayertwo() );  // OK!
    //output ( getplayerone,   getplayertwo );    // wrong!
    // 2.
    output1 ( getplayerone, getplayertwo );       // pass the function pointers

    return 0;
}

int getplayerone (void)
{
    return 1;
}

int getplayertwo (void)
{
    return 2;
}

void output (int ret_getplayerone, int ret_getplayertwo)
{
 printf ("Output()   %d , %d\n", ret_getplayerone, ret_getplayertwo );
}

void output1 (int (*f1)(), int (*f2)() )
{
 printf ("\nOutput1()  %d , %d\n", (*f1)(), (*f2)() );
}

Output:

输出:

Output()   1 , 2
Output()   1 , 2

Output1()  1 , 2

#3


0  

The function prototype is wrong, it should be

函数原型是错误的,它应该是

void output(int playerone, int playertwo);

#1


1  

you call output this way:

你用这种方式调用输出:

output( getplayerone(),  getplayertwo());

call it with the functions itself:

用函数本身调用它:

output( getplayerone,  getplayertwo);

#2


1  

Why can't I use a function return value as a parameter?

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

You can. This is the proper call:

您可以。这是正确的电话:

output( getplayerone(),  getplayertwo());

If

如果

void output(int r1,int r2); // also `void output(int, int);`  would do
void output(int r1,int r2){
//...
}

Since you declared your function output as:

因为您将函数输出声明为:

void output(int getplayerone(),int getplayertwo());
void output(int getplayerone(),int getplayertwo()){
//...
}

You need to pass function pointers as parameters:

您需要将函数指针作为参数传递:

   output(getplayerone, getplayertwo);

The small example program:

小例子程序:

#include <stdio.h>

int getplayerone (void);
int getplayertwo (void);

void output (int ret_getplayerone, int ret_getplayertwo);
void output1 (int (*f1)(), int (*f2)() );

int main (int argc, char *argv[])
{
    int r1, r2;
    // 1.
    output (r1=getplayerone(), r2=getplayertwo());// OK, but there is no need to do so 
    output ( getplayerone(),   getplayertwo() );  // OK!
    //output ( getplayerone,   getplayertwo );    // wrong!
    // 2.
    output1 ( getplayerone, getplayertwo );       // pass the function pointers

    return 0;
}

int getplayerone (void)
{
    return 1;
}

int getplayertwo (void)
{
    return 2;
}

void output (int ret_getplayerone, int ret_getplayertwo)
{
 printf ("Output()   %d , %d\n", ret_getplayerone, ret_getplayertwo );
}

void output1 (int (*f1)(), int (*f2)() )
{
 printf ("\nOutput1()  %d , %d\n", (*f1)(), (*f2)() );
}

Output:

输出:

Output()   1 , 2
Output()   1 , 2

Output1()  1 , 2

#3


0  

The function prototype is wrong, it should be

函数原型是错误的,它应该是

void output(int playerone, int playertwo);