Some questions with pointers

This is a discussion on Some questions with pointers within the C Programming forums, part of the General Programming Boards category; I am doing the card dealing function (from Deitel's C (thick) book) The function receives the address of the shuffled ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    Some questions with pointers

    I am doing the card dealing function (from Deitel's C (thick) book)
    The function receives the address of the shuffled deck to pick from, the address (the player's hand) and a pointer of the last card picked( ranges from 1 to 52).
    Whenever the dealFive function is called it fills in the hand array with the 5 cards (suit and face in int), and should update the pointer's content, *iPtr.
    So here's the code:
    Code:
    void dealFive(int wDeck[][13],int hand[5][2], int *iPtr){
        int row, col;
        for (;(*iPtr)<((*iPtr)+5); (*iPtr)++)
            {
                for (row =0; row < 4; row++)
                {
                    for (col = 0; col < 13; col++)
                    {
                        if (wDeck[row][col]==(*iPtr))
                        {
                            hand[(*iPtr)][0] = row; //suit
                            hand[(*iPtr)][1] = col; //face
                            printf("Card %d: %d %d\n",*iPtr, row,col);
                            break;
                        }
                    }
                }
            }
    }
    I wish to use only pointer basics and arrays.


    The problem is that the dereferenced pointer resets to 2 after incrementing from 4. The values of the pointed address messes up afterwards. The function does not terminate at all.
    I must have made a mistake. I need advice with using pointers. Thank you.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    for (;(*iPtr)<((*iPtr)+5); (*iPtr)++)
    I'm not sure what you want to do, but your condition is definitely wrong because the value "iPtr" points to will always be smaller than the same value + 5 until the value will be INT_MAX - 4.

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Thanks for the reply.
    I have addressed the *iPtr+5 issue.
    Code:
    void dealFive(int wDeck[][13],int hand[5][2], int *iPtr){
        int row, col;
        int fivecards;
        fivecards = (*iPtr)+5;
        for (;(*iPtr)<fivecards; (*iPtr)++)
            {
                for (row =0; row < 4; row++)
                {
                    for (col = 0; col < 13; col++)
                    {
                        if (wDeck[row][col]==(*iPtr))
                        {
                            hand[(*iPtr)][0] = row; //suit
                            hand[(*iPtr)][1] = col; //face
                            printf("Card %d: %d %d\n",*iPtr, row,col);
                            break;
                        }
                    }
                }
            }
    }
    What I did was to store the current *iPtr+5 to a variable.
    Code:
    Deck initialized. 1  2  3  4  5  6  7  8  9 10 11 12 13
    ---------------------------------------
     1  2  3  4  5  6  7  8  9 10 11 12 13
    14 15 16 17 18 19 20 21 22 23 24 25 26
    27 28 29 30 31 32 33 34 35 36 37 38 39
    40 41 42 43 44 45 46 47 48 49 50 51 52
    ---------------------------------------
    Deck shuffled.
     1  2  3  4  5  6  7  8  9 10 11 12 13
    ---------------------------------------
    30 18 47 17 27 44  2 42 11 16 50 48 37
     3  9 19 52 20 28 39  8 41 13 33  6 23
    12 34 45 29 38 43 26 32 40 31 15  7 24
    51 46 35  1 25 36 10 22  4  5 21 49 14
    ---------------------------------------
    Card 1: 3 3
    Card 2: 0 6
    Card 3: 1 0
    Card 4: 3 8
    Card 3: 3 9
    The above shows the runtime terminal.
    Then, tracking the program, as the pointed value reaches 4, somewhere in the code, it becomes 3, but its the right card based on suit number and face number, and consequently, runs infinitely. I was wondering if the if statement using *iPtr, modifies it.
    I want to practice the pass-by-reference by using pointers. In this function, the main program calls dealFive and sends the pointer. The address contains the number of cards picked from the shuffled deck, so when I call the deal function again, it won't start from the first card in the array, wDeck.
    For instance, two players play a card game (like poker). Each player N (int handN[][]) would receive 5 cards from the shuffled deck. I thought that what if I would only pick a card? I thought of using pointer of the number of cards drawn until it reaches 52, incrementing it everytime a card is drawn/dealt.
    Afterwards, I would add some logical check for "no more cards left".

    I would appreciate some advice/changes for the code.

    Again, thank you.
    Last edited by kirchhoff; 11-26-2012 at 04:05 AM.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,099
    Personally I would find it clearer to give iPtr a name which implies its purpose, and to drop variables which do not add clarity like "fivecards". Also your convention of storing suits in col 0 and faces in col 1 would be good to define as named constants to ensure that you use the same convention across all functions in your program.

    Code:
    #define SUIT 0
    #define FACE 1
    void dealFive(int wDeck[][13], int hand[5][2], int *card_id)
    {
        for (int crd = 0; crd < 5; i++)
        {
            for (int row=0; row < 4; row++)
            {
                for (int col=0; col < 13; col++)
                {
                    if (wDeck[row][col]==(*card_id))
                    {
                        hand[(*card_id)][SUIT] = row; 
                        hand[(*card_id)][FACE] = col; 
                        printf("Card %d: %d %d\n", *card_id, row, col);
                        (*card_id)++;  
                        break;
                    }
                }
            }
        }
    }
    Last edited by c99tutorial; 11-26-2012 at 11:48 AM.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by kirchhoff View Post
    Then, tracking the program, as the pointed value reaches 4, somewhere in the code, it becomes 3, but its the right card based on suit number and face number, and consequently, runs infinitely. I was wondering if the if statement using *iPtr, modifies it.
    You have a typical array overrun.
    I guess your initial value for (*iPtr) is 1, right?

    Now look at your code:
    Code:
    void dealFive(int wDeck[][13],int hand[5][2], int *iPtr)
    {
    ...
             hand[(*iPtr)][0] = row; //suit
             hand[(*iPtr)][1] = col; //face
    Look at the first dimension of "hand" and then think about what happens when (*iPtr) is 5. Do you understand the array overrun?
    It looks like when writing outside your "hand" array you are accessing the memory block "iPtr" points to.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Quote Originally Posted by AndiPersti View Post
    You have a typical array overrun.
    I guess your initial value for (*iPtr) is 1, right?
    Yes, it was 1.

    Thank you for the help, c99tutorial and Andreas.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two questions about pointers
    By Dynesclan in forum C Programming
    Replies: 4
    Last Post: 06-10-2012, 11:55 AM
  2. A few questions on pointers
    By dgs012 in forum C Programming
    Replies: 1
    Last Post: 12-01-2010, 08:53 AM
  3. Questions about pointers and malloc
    By H`eya in forum C Programming
    Replies: 4
    Last Post: 04-19-2010, 07:50 PM
  4. questions about pointers
    By radxxx in forum C Programming
    Replies: 3
    Last Post: 04-21-2009, 11:38 AM
  5. Some questions about pointers
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2002, 02:25 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21