简体   繁体   English

为什么函数不能使用指针呢?

[英]Why function doesn't work with that use of pointer?

I don't know how i should use pointer, to take value of "win" in function main from function spr(). 我不知道我该如何使用指针来从函数spr()获取函数main中的“ win”值。 Should I define one more var for example resullt and write it as: 我是否应该再定义一个var例如resullt并将其写为:

bool *result;
result=&win;

The whole program should work fine, but I can't manage with using pointers. 整个程序应该可以正常工作,但是我无法使用指针进行管理。 What do you think? 你怎么看? Thanks for help! 感谢帮助!

bool spr(char arr[3][3], bool *win)
{
    if(arr[0][0]==arr[0][1] && arr[0][1]==arr[0][2] || arr[1][0]==arr[1][1] && arr[1][1] ==arr[1][2] || arr[2][0]==arr[2][1] && arr[2][1]==arr[2][2])
    {
        *win=true;      

    }
    else if(arr[0][0]==arr[1][0] && arr[1][0]==arr[2][0] || arr[0][1]==arr[1][1] && arr[1][1]==arr[2][1] || arr[0][2]==arr[1][2] && arr[1][2]==arr[2][2])
    {
        *win=true;

    }
    else if(arr[0][0]==arr[1][1] && arr[1][1]==arr[2][2] || arr[0][2]==arr[1][1] && arr[1][1]==arr[2][0])
    {
        *win=true;

    }
    return *win;
    printf("wygrales");
}

int main()
{
    char arr[3][3]={{' ',' ',' '},{' ',' ',' '}};
    int x1,x2,y1,y2;
    int kolejka=0;
    printf("GRA W KOLKO I KRZYZYYK!");
    Sleep(2000);
    system("cls");


    while((&win)!=true || kolejka <=9)
    {
        printf(" KOLKO: Wprowadz wspodlrzedne: \n");
        scanf("%d",&x1);
        scanf("%d",&y1);
        arr[x1][y1]='O';

        spr(arr,&win);
        wypisz(arr);

        printf("KRZYZYK: Wprowadz wspolrzedne:\n ");
        scanf("%d",&x2);
        scanf("%d",&y2);
        arr[x2][y2]='X';

        spr(arr,&win);
        wypisz(arr);
        kolejka++;
    }
}

you do not declare win anywhere. 您不会在任何地方宣布获胜。 BTW the printf("wygrales"); 顺便说一句printf("wygrales"); will never be reached as you return from the function before it. 当您从之前的函数返回时,它将永远不会到达。

int main()
{
  ...
  bool win = false;

  ...
  while(!win || kolejka <=9)
  ...

But you do not need to pass pointer to win to the function spr . 但是您不需要传递指针就可以赢得函数spr Just return true if your long if s are ok or false at the end of the function. 如果在函数末尾, if s正确或错误,只要返回true即可。 You do not this pointer at all. 您根本不需要此指针。

You will need to change 您将需要改变

spr(arr,&win); -> win = spr(arr); -> win = spr(arr);

and declare win in the main as in my first part of the answer; 并像我回答的第一部分一样,在主要方面宣布胜利;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM