Reputation: 125
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 specific poker hand. If activated, the function prints the line "Player 1 has a full house!" or whatever the hand might be. But, i also need to increment the value of p1, in which p1 is a global variable the holds the total score for p1.
Printing the line works perfectly, but when i want to assign, for example, a value of 10 to p1, it simply doesn't assign.
In the example below, the printfs work perfectly when they should, but the px's don't assign. I've even printed the value for px immediately after each function and it still prints 0s.
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;
}
}
Upvotes: 3
Views: 131
Reputation: 2679
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]
Upvotes: 4