简体   繁体   中英

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(). Should I define one more var for example resullt and write it as:

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"); 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 . Just return true if your long if s are ok or false at the end of the function. You do not this pointer at all.

You will need to change

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

and declare win in the main as in my first part of the answer;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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