Yes, I know it sounds silly but i have no idea what i\'m doing wrong!
The function is part of a poker game, in which there are 10 functions, each which checks for a
You're passing the parameter "by value" and not "by reference". This means that once you pass px
to the function you have a copy of it inside the function, so any modification inside the function won't affect the original px
.
Try with this (see we are now passing to the function the parameter as a pointer):
void checkForPoker(int j, int* px) //j is the player's number, px is the player's scoreholder
{
if ((c1==c2 && c2==c3 && c3==c4) || (c1==c2 && c2==c3 && c3==c5) || (c1==c2 && c2==c4 && c4==c5) || (c1==c3 && c3==c4 && c4==c5))
{
printf("\n\nEl Jugador %d tiene un poker de %ss!", j, traducirCarta(c1));
*px = 8;
}
if (c5==c2 && c2==c3 && c3==c4)
{
printf("\n\nEl Jugador %d tiene un poker de %ss!", j, traducirCarta(c2));
*px = 8;
}
}
This implies also a change in the calling code. Instead of passing the integer you will have to pass the address to the integer, something like:
Instead of
int a = 2;
checkForPoker(2, a);
You will have to do something like:
int a = 2;
checkForPoker(2, &a);
As suggested by a SO user (Charlon) you could choose another approach, avoiding the use of pointers: you can use px
as return value of the function:
int checkForPoker(int j) //j is the player's number
{
int px = 0;
if ((c1==c2 && c2==c3 && c3==c4) || (c1==c2 && c2==c3 && c3==c5) || (c1==c2 && c2==c4 && c4==c5) || (c1==c3 && c3==c4 && c4==c5))
{
printf("\n\nEl Jugador %d tiene un poker de %ss!", j, traducirCarta(c1));
px = 8;
}
if (c5==c2 && c2==c3 && c3==c4)
{
printf("\n\nEl Jugador %d tiene un poker de %ss!", j, traducirCarta(c2));
px = 8;
}
return px;
}
And then you could assign player's scoreholder like this:
player->scoreholder = checkForPoker(int j) //j is the player's number
Please note that I'd stick to the first approach for performance reasons (superfluous copies in the second approach).
For an extended reading on the subject you could find useful these links: [1] [2]