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?
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?
– lucas pelepek
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.
– Maniero