Thread: Function writes to array for no reason

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    22

    Question Function writes to array for no reason

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall the difference between = and == as seen here:
    Code:
    if (ptr->value = 1)
    if you compile at a sufficiently high warning level, your compiler probably would have warned you about this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    if (ptr->value = 1)
    You want == (comparison), not = (assignment).

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    1. Provide us with enough code to compile and run your program, and sample input that demonstrates the problem along with what you expect the output to be and the actual output you get. This saves us a ton of time and effort; it helps us help you.

    2. Compile at the maximum warning level (e.g. -Wall option for gcc):
    Code:
    $ make cards
    gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o cards cards.c -lm -lpthread -lrt
    cards.c: In function ‘calcSum’:
    cards.c:17:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
             if (ptr->value = 1)
             ^
    cards.c:46:22: warning: variable ‘ptr2’ set but not used [-Wunused-but-set-variable]
             struct card *ptr2;
                          ^
    That first warning is the cause of overwriting value. A single = is used for assignment, a double == is used for comparison.
    That second warning suggests you are doing something wrong in that else block. You declared ptr2, and you point it to elements of the array, but you never use ptr2. You never access the data it points to. Perhaps you meant sum += ptr2->value?

    Still, there's no reason to use pointers here, it just confuses things. You can simply do something like sum += playerHand[i];

    Given the "21" on line 28, I'm guessing this is a blackjack program. If that's the case, I think you're not correctly handling the case where the player has more than one ace. What should aceBigSum be if there are two aces? What about three?

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Why are you still using a size of 9 when I told you in another thread that the maximum possible number of cards in a blackjack hand is 11?

    Anyway, your code is overcomplicated.
    Code:
    int sum = 0, acefound = 0;
    for (int i = 0; i < ncards; i++) {
        sum += hand[i].value;
        if (hand[i].value == 1)
            acefound = 1;
    }
    return (acefound && sum + 10 <= 21) ? sum + 10 : sum;
    @anduril, only one ace will ever have the value 11 since two 11's is 22.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by algorism View Post
    @anduril, only one ace will ever have the value 11 since two 11's is 22.
    But I like to live dangerously.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-23-2012, 11:29 AM
  2. Replies: 3
    Last Post: 10-23-2011, 09:18 PM
  3. Writes twice to disk, why?
    By JonathanS in forum C Programming
    Replies: 5
    Last Post: 10-13-2011, 12:29 AM
  4. only writes 'w' in new file
    By krieghart in forum C Programming
    Replies: 2
    Last Post: 12-04-2010, 11:55 PM
  5. Replies: 16
    Last Post: 09-23-2008, 03:32 AM