Code:
int calcSum(struct card playerHand[], int noOfCards) {
int sum = 0;
int numberOfAces = 0;
struct card *ptr;
for (int j = 0; j < noOfCards; j++)
{
ptr = &playerHand[j];
if (ptr->value = 1)
{
numberOfAces++;
}
}
if (numberOfAces != 0) {
int aceBigSum = 10, aceSmallSum = 0;
for (int i = 0; i < noOfCards; i++)
{
ptr = &playerHand[i];
aceSmallSum += ptr->value;
}
for (int i = 0; i < noOfCards; i++)
{
ptr = &playerHand[i];
aceBigSum += ptr->value;
}
if ((aceBigSum < aceSmallSum) || (aceBigSum > 21))
{
return aceSmallSum;
}
else
{
return aceBigSum;
}
}
else {
struct card *ptr2;
for (int i = 0; i < noOfCards; i++)
{
ptr2 = &playerHand[i];
sum += ptr->value;
}
return sum;
}
}
So, I have this function which gets passed a struct array (of size 9, if that matters) and an int showing how many elements have been assigned values.
For some reason it seems to write 1 to playerHand[0].value and playerHand[1].value, which I don't want it to do. I want the array to keep the same values it had when the call happened.
The struct looks like this:
Code:
struct card {
int suit;
int value;
};
The call looks like this:
Code:
calcSum(player1hand,2)
If i write the function like this everything seems to work fine, but it doesn't do what I want the function to do of course:
Code:
int calcSum(struct card playerHand[], int noOfCards) {
int sum = 0;
struct card *ptr;
for (int i = 0; i < noOfCards; i++)
{
ptr = &deckAr[i];
sum += ptr->value;
}
return sum;
}
So any ideas? I cant find any clue as to why it would write anything to the array.
/ Finoli