0

So to keep it short, I have a problem that is making me change around a pre-built program to include pointers.

It's a deck of cards and two hands.

I'm (trying) to make it so that instead of copying the values of the deck and putting them into the hand, I will just have the hand point to the card in the deck. I know it's not really how decks work, but the point of the exercise is just to get used to pointers, not the practicality of it (The way the program actually sets the decks makes me want to rip my hair out).

I almost have the basic syntax down of having a Ptr be set to the memory of the top of the deck, which is then stored into the hand.

However, the issue comes in when I have the hand array. The hand isn't equal to the deck memory, it's the next available memory, but it still has the same value, which means I'm not pointing to it, I'm copying it.

Here is the proto-function

int dealHand(int wDeck[][FACES], int *wHand[HAND], int top);

Here are the definitions and the call from main:

int *hand1[HAND] = {0};
int topDeck = 1;

topDeck = dealHand(deck, hand1, topDeck);

Here is the function:

int dealHand(int wDeck[][FACES], int *wHand[HAND], int top) {
  size_t hand;
  size_t row;
  size_t col;
  int *arrayPtr;

  for (hand = 0; hand < HAND; hand++) {
    for (row = 0; row < SUITS; row++) {
      for(col = 0; col < FACES; col++) {
        if (wDeck[row][col] == top) {
          printf("%d %d\n", row, col);                  

          arrayPtr = &(wDeck[row][col]);
          wHand[hand] = arrayPtr; 

          printf("   DECK: %p\n", &wDeck[row][col]);
          printf("   DECK: %d\n", wDeck[row][col]);

          printf("POINTER: %p\n", arrayPtr);
          printf("POINTER: %d\n", *arrayPtr);

          printf("   HAND: %p\n", &wHand[hand]);
          printf("   HAND: %d\n", wHand[hand]);
        }  // if
      }  // row
    }  // col
   top++;
  }  // hand
  return top;
}
5
  • The parentheses around wDeck and wHand are all redundant Commented Apr 19, 2014 at 0:56
  • Can you give some explanation of what your setup is? What do the values in the wDeck 2-D array represent, and what is HAND and why? Commented Apr 19, 2014 at 1:11
  • Basically the way the book wrote the program is that when it shuffles the deck it is also initializing it. It does this by doing a random number everytime a card creation is called. Two random numbers are created, and they represent the face and suit. When those two numbers are called they are stored in the row and column of the deck array, with the value of the cords being the turn they were generated. So when I call the top card of the deck I'll get that value,lets say it's the sixth card, look up in the deck array which cord has the value of six, and read the row/col to see what card it is Commented Apr 19, 2014 at 1:34
  • So if wDeck[0][4] == 6 it means that the 5 of spades was the 6th card generated? What happens if the random numbers identify a card that has already been done? Commented Apr 19, 2014 at 1:40
  • The process is in a for loop that creates 52 cards. Within that for loop is a do/while loop that generates the cords and checks if the card is generated or something. Like I said, I didn't write the whole thing, I'm just modifying it. Commented Apr 19, 2014 at 1:44

1 Answer 1

2

In order to store the pointers, the wHand array must be an array of pointers:

// wDeck is a 2-dimensional array of ints.
// wHand is an array of pointers to ints.
void dealHand(int wDeck[][FACES], int *wHand[HAND]) {

The wHand array has size HAND, so its indices run from 0 to HAND - 1. It's an error for the for loop to access wHand[HAND]:

for (hand = 0; hand <= HAND; hand++)  // Error: should be 0 to HAND-1.

The arrayPtr is a pointer into the wDeck array, so it must contain the address of an element:

arrayPtr = &(wDeck[row][col]);  // Parens are optional.

Then the address should be copied into the wHand array, which now contains pointers, not ints:

wHand[hand] = arrayPtr;

Note that arrays are generally referenced without parentheses:
name[i][j] and not (name)[i][j]:

printf("   DECK: %p\n", &wDeck[row][col]);
printf("   DECK: %d\n", wDeck[row][col]);
// Similarly for printing the HAND data.

Edit:

Now that you've changed the wHand[] array to contain pointers, you'll also need to update the way you print its values:

printf("   HAND: %p\n", wHand[hand]);  // Each element is a pointer.
printf("   HAND: %d\n", *wHand[hand]);  // Print the int it points to.
Sign up to request clarification or add additional context in comments.

2 Comments

I made the changes but the hand array still isn't working as intended. I will edit the original post to show more of the code in order to identify the issue.
Please see sscce.org to learn how to provide the information we need to help you. Please also show the expected and actual output; "isn't working" isn't specific enough for use to understand the problem. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.