Thread: Getting odd values from printf

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

    Getting odd values from printf

    Hi, it's the guy with the card game code again...

    main():
    Code:
    #include <stdlib.h>#include <stdio.h>
    #include <time.h>
    #include "bjfunctions.h"
    
    
    
    
    int main()
    {
        srand((unsigned int)time(NULL)); //initializing srand
    
    
        struct card *deckAr = createDeck(); //creating the struct card deck array
    
    
        for (int i = 0; i < 100000; i++)
        {
            shuffleDeck(deckAr);
        }
            struct card *player1hand = (struct card*)malloc(sizeof(player1hand));
            struct card *player2hand = (struct card*)malloc(sizeof(player2hand));
            struct card *househand = (struct card*)malloc(sizeof(househand));
    
    
            int player1NCards = 0, player2NCards = 0, houseNCards = 0;
                
    
    
            player1NCards = 2;
            deal(player1hand, deckAr, player1NCards, 2);
            for (int j = 0; j < player1NCards; j++){
            printf("Card %d %d of %d\n\n", j+1, player1hand[j].value, player1hand[j].suit);
            }
    
    
            for (int j = 0; j < player1NCards; j++) {
                printf("Card %d %d of %d\n\n", j + 1, deckAr[j].value, deckAr[j].suit);
            }
    
    
    
    
        return 0;
    }
    bjfunctions.c:
    Code:
    #include "bjfunctions.h"#include <stdlib.h>
    #include <stdio.h>
    
    
    void shuffleDeck(struct card *deck) {
        int cardSwitch1, cardSwitch2;
        struct card temp;
        cardSwitch1 = (rand() % 52);
        cardSwitch2 = (rand() % 52);
        temp = deck[cardSwitch1];
        deck[cardSwitch1] = deck[cardSwitch2];
        deck[cardSwitch2] = temp;
    }
    
    
    const char *suitName(const int suitNum)
    {
        switch (suitNum) {
        case 1: return "Spades";
        case 2: return "Clubs";
        case 3: return "Hearts";
        case 4: return "Diamonds";
        }
    
    
        return "(invalid suit)";
    }
    /*
    int dealNCards(struct card playerHandAr[], struct card deckAr[], int N, int nOfCards)
    {
        for (int i = 0; i < N; i++)
        playerHandAr[nOfCards] = deckAr[0];
        moveToBack(deckAr);
        nOfCards++;
    
    
        return nOfCards;
    }
    
    
    void moveToBack(struct card deckAr[]) {
        struct card temp;
        for (int i = 0; i < 52; i++){
        temp = deckAr[0];
        }
    }*/
    
    
    const char *valueName(const int valueNum)
    {
        switch (valueNum) {
        case 1: return "Ace";
        case 2: return "Two";
        case 3: return "Three";
        case 4: return "Four";
        case 5: return "Five";
        case 6: return "Six";
        case 7: return "Seven";
        case 8: return "Eight";
        case 9: return "Nine";
        case 10: return "Ten";
        case 11: return "Jack";
        case 12: return "Queen";
        case 13: return "King";
        }
    
    
        return "(invalid value)";
    }
    
    
    void *deal(struct card *ptr, struct card *deckArray, int currentNofCards, int nOfCards)
    {
        ptr = (struct card *)realloc(ptr, sizeof(ptr) * currentNofCards + nOfCards);
        for (int i = currentNofCards; i < nOfCards+currentNofCards; i++)
        { 
        ptr[i+currentNofCards] = deckArray[i];
        }
        
    }
    
    
    struct card makeCard(int suit, int value) {
    
    
        struct card tempCard;
        tempCard.suit = suit;
        tempCard.value = value;
    
    
        return tempCard;
    }
    
    
    struct card *createDeck()
    {
        const size_t  count = 52;
        struct card *deck;
    
    
    
    
        // Allocate new deck of 'count' cards. 
        deck = malloc(count * sizeof deck[0]);
        /*if (!deck) {
            fprintf(stderr, "createDeck(): Out of memory!\n");
            exit(EXIT_FAILURE);
        }*/
    
    
        int i = 0;
        struct card *deckArrayPtr = deck;
        for (int suit = 1; suit < 5; suit++)
        {
            {
                for (int value = 1; value < 14; value++) {
                    deck[i] = makeCard(suit, value);
                    i++;
                }
            }
        }
        return deck;
    }
    Output is something like:


    Card 1 -33686019 of -842150451


    Card 2 -394304501 of -1646174777


    Card 1 3 of 1


    Card 2 9 of 4


    Press any key to continue . . .



    any ideas? I've been at this piece of ........ function for hours


    Code:
    #pragma once
    
    struct card {
    	int suit;
    	int value;
    };
    void shuffleDeck(struct card *deck);
    
    
    int dealNCards(struct card deckAr[], int N, int nOfCards);
    
    
    moveToBack(struct card deckAr[]);
    
    
    struct card *createDeck();
    
    
    struct card makeCard(int suit, int value);
    
    
    const char *suitName(const int suitNum);
    
    
    const char *valueName(const int valueNum);
    
    
    void deal(struct card *ptr, struct card *deckArray, int currentNofCards, int nOfCards);
    Last edited by Finoli; 01-05-2016 at 08:47 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You didn't include "bjfunctions.h", so we can't see your struct declaration. Nor can we compile the code.

    Are you getting any warnings when you compile? Make sure you maximize your compiler warnings.

    Just looking at "main()":

    Code:
    struct card *player1hand = (struct card*)malloc(sizeof(player1hand));
    struct card *player2hand = (struct card*)malloc(sizeof(player2hand));
    struct card *househand = (struct card*)malloc(sizeof(househand));
    Don't cast the return value of "malloc()": FAQ > Casting malloc - Cprogramming.com
    You're not allocating the correct memory - you should be dereferencing the pointers:

    Code:
    struct card *player1hand = malloc(sizeof(*player1hand));
    You should also be checking that memory has been successfully allocated before attempting to access it, and freeing it when you are done.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    22
    Quote Originally Posted by Matticus View Post
    You didn't include "bjfunctions.h", so we can't see your struct declaration. Nor can we compile the code.

    Are you getting any warnings when you compile? Make sure you maximize your compiler warnings.

    Just looking at "main()":

    Code:
    struct card *player1hand = (struct card*)malloc(sizeof(player1hand));
    struct card *player2hand = (struct card*)malloc(sizeof(player2hand));
    struct card *househand = (struct card*)malloc(sizeof(househand));
    Don't cast the return value of "malloc()": FAQ > Casting malloc - Cprogramming.com
    You're not allocating the correct memory - you should be dereferencing the pointers:

    Code:
    struct card *player1hand = malloc(sizeof(*player1hand));
    You should also be checking that memory has been successfully allocated before attempting to access it, and freeing it when you are done.
    I've fixed the allocation-statements, no change in output.
    "bjfunctions.h" is not in the bottom of the original post.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What do you expect the output to be?

    You may want to insure all of your variables are initialized, such large numbers suggest something may not be initialized before use.

    Also why have you declared your deal function to return a pointer, you aren't returning this promised value from that function.

    Jim

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    22
    Quote Originally Posted by jimblumberg View Post
    What do you expect the output to be?
    I expect the first Card 1 and Card 2 to be the same as the second set of Card 1 and 2.
    Quote Originally Posted by jimblumberg View Post
    You may want to insure all of your variables are initialized, such large numbers suggest something may not be initialized before use.

    Also why have you declared your deal function to return a pointer, you aren't returning this promised value from that function.

    Jim
    struct card *player1hand = malloc(sizeof(*player1hand));
    Do i need to initialize player1hand somehow?

    Also i dont know why I did that, I got rid of it now.
    Last edited by Finoli; 01-05-2016 at 08:24 AM.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Finoli View Post
    "bjfunctions.h" is not in the bottom of the original post.
    So perhaps you would care to post it?

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you using malloc in main, why not just use a pointer?
    Code:
            struct card *player1hand = NULL;
    In your deal function you seem to be trying to create an array. But since you're passing the pointer by value that realloc() call in the function is working on a copy of the pointer so any changes made within that function will be lost when the function returns.

    Jim

  8. #8
    Registered User
    Join Date
    Dec 2015
    Posts
    22
    Quote Originally Posted by Matticus View Post
    So perhaps you would care to post it?
    Done, I did add it but i guess it didnt get saved.
    Quote Originally Posted by jimblumberg View Post
    Why are you using malloc in main, why not just use a pointer?
    Code:
            struct card *player1hand = NULL;
    In your deal function you seem to be trying to create an array. But since you're passing the pointer by value that realloc() call in the function is working on a copy of the pointer so any changes made within that function will be lost when the function returns.

    Jim
    well i heard realloc only works on stuff previously allocated with malloc but I guess that's incorrect then?

    About the deal funktion, I understand but I'm not sure why it's passed-by-value, since i thought I passed by reference.
    Would be great if you showed me where the error lies.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    well i heard realloc only works on stuff previously allocated with malloc but I guess that's incorrect then?
    No that's correct, your implementation is what is wrong. The question is why are you using realloc()? Why are you allocating a single instance in main() with malloc then almost immediately try to create an array in the function? Why not just pass the pointer into the function, malloc() the array of the correct dimensions and be done with it?

    I understand but I'm not sure why it's passed-by-value, since i thought I passed by reference.
    You passed the value by "reference" but you passed the pointer by value. You're trying to change the pointer so you must pass a pointer to that pointer to the function.

    Jim

  10. #10
    Registered User
    Join Date
    Dec 2015
    Posts
    22
    Quote Originally Posted by jimblumberg View Post
    No that's correct, your implementation is what is wrong. The question is why are you using realloc()? Why are you allocating a single instance in main() with malloc then almost immediately try to create an array in the function? Why not just pass the pointer into the function, malloc() the array of the correct dimensions and be done with it?


    You passed the value by "reference" but you passed the pointer by value. You're trying to change the pointer so you must pass a pointer to that pointer to the function.

    Jim
    The reason is that the deal function will be called several times depending on how many cards the player wants.

    Im not sure how to do that (note im still a beginner) would you please correct the code or show me where in the code the problem lies?

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The problem starts here:

    Code:
    void deal(struct card *ptr, struct card *deckArray, int currentNofCards, int nOfCards)
    If you want to change the pointer you need to pass a pointer to the pointer:
    Code:
    void deal(struct card **ptr, struct card *deckArray, int currentNofCards, int nOfCards)
    After you make this change you'll need to change the function to take this change into consideration.

    How many cards can the player request? What is the maximum number of cards that a player can hold?

    Jim

  12. #12
    Registered User
    Join Date
    Dec 2015
    Posts
    22
    But... do I want to change the pointer itself? I want to change the values of the elements of the array the pointer points to. I thought that was what I was doing?


    The player can indeed take a limited amount of cards. I just thought it would make more sense to increase the array as the player requested more cards. The maximum is 9 cards if my math is right.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But... do I want to change the pointer itself?
    You're calling realloc() so it seems that you want to change the pointer, that's what realloc() does.

    I want to change the values of the elements of the array the pointer points to. I thought that was what I was doing?
    Then pass an array of the proper size to the function. Right now you are passing a pointer to a single element.

    The player can indeed take a limited amount of cards.
    Then why are you playing with dynamic memory? Just allocate an array large enough to hold the maximum number of cards.

    The maximum is 9 cards if my math is right.
    What game are you trying to play?

    Jim

  14. #14
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Finoli View Post
    The maximum is 9 cards if my math is right.
    If this is blackjack, then the maximum number of cards possible in a hand without going bust is 11:
    1 + 1 + 1 + 1 + 2 + 2 + 2 + 2 + 3 + 3 + 3 = 21

  15. #15
    Registered User
    Join Date
    Dec 2015
    Posts
    22
    Quote Originally Posted by jimblumberg View Post
    You're calling realloc() so it seems that you want to change the pointer, that's what realloc() does.


    Then pass an array of the proper size to the function. Right now you are passing a pointer to a single element.


    Then why are you playing with dynamic memory? Just allocate an array large enough to hold the maximum number of cards.


    What game are you trying to play?

    Jim
    I decided to do just that, declare a static array of structs. However I'm having issues changing the elements of it inside deal().
    Mind telling me how you'd do it? I've tried everything I can think of and nothing's working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 07-27-2013, 10:20 AM
  2. write() and read() int values from socket returning wrong values.
    By Premjith P S in forum Linux Programming
    Replies: 8
    Last Post: 11-29-2012, 02:59 PM
  3. Replies: 2
    Last Post: 11-02-2011, 04:28 PM
  4. printing null values with printf
    By django in forum C Programming
    Replies: 12
    Last Post: 08-23-2011, 08:24 AM
  5. printf returning values problem
    By hayai32 in forum C Programming
    Replies: 20
    Last Post: 06-25-2005, 12:16 PM

Tags for this Thread