"strcpy" behaving strangely depending on how you put it in "if"

Asked

Viewed 83 times

0

What’s going on here?

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

void escolha_simb(char *jog1, char *jog2)   //** escolhe quem vai ser x ou o
{
        char esc;

        while (1) {
                printf("\njogador 1, escolha X ou O \n");
                fflush(stdin);
                scanf("%c",&esc);
                if (esc == 'x')
                {
                        strcpy(jog2,"O");
                        strcpy(jog1,"X");

                        break;
                }
                else if (esc == 'o')
                {
                        strcpy(jog2,"X");
                        strcpy(jog1,"O");

                        break;
                }
        }
}

int main(int argc, char const *argv[]) {
        char jog1=0,jog2=0;
        printf("jog1: %c jog2: %c",jog1,jog2 );
        escolha_simb(&jog1,&jog2);
        printf("depois\njog1: %c jog2: %c",jog1,jog2 );
        return 0;
}

Output, if choose X:

jog1:   jog2:
jogador 1, escolha X ou O
x
depois
jog1: X jog2: O

but if it reverses the strcpy():

 if (esc == 'x')
                {
                        strcpy(jog1,"X");
                        strcpy(jog2,"O");


                        break;
                }
                else if (esc == 'o')
                {
                        strcpy(jog1,"O");
                        strcpy(jog2,"X");


                        break;
                }

output, if choose X :

jog1:   jog2:
jogador 1, escolha X ou O
x
depois
jog1:   jog2: O

why jog1 is void?

1 answer

0


Is mixing char with string, are different things. When you use char should not even use strcpy(), or assign using double quotes that refer to strings. It will not give problem always, but the coded form is not correct, so it is more correct:

#include <stdio.h>

void escolha_simb(char *jog1, char *jog2) {
    while (1) {
        printf("\njogador 1, escolha X ou O \n");
        char esc;
        scanf("%c", &esc);
        if (esc == 'x') {
            *jog2 = 'X';
            *jog1 = 'O';
            break;
        }
        else if (esc == 'o') {
            *jog2 = 'O';
            *jog1 = 'X';
            break;
        }
    }
}

int main() {
    char jog1 = 0, jog2 = 0;
    printf("jog1: %c jog2: %c", jog1, jog2);
    escolha_simb(&jog1, &jog2);
    printf("depois\njog1: %c jog2: %c", jog1, jog2);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

You probably have more performative ways of doing this, but you’d need to test.

  • got it, I’ll use your method. but even so, is it random to give null? because just doing an inversion of them gives the correct result?

  • I wouldn’t say random, but it’s hard to know when it happens. In C you need to understand everything that goes on in memory, every byte that is putting in each place, understand every detail of how the language works. It is a flexible and powerful language, and can give a good sense of the actual functioning of codes, but it is not simple to use in most applications or who does not want to delve into.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.