user50017
user50017

Reputation: 3

Making Array point to memory of another array C

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;
}

Upvotes: 0

Views: 3036

Answers (1)

Adam Liss
Adam Liss

Reputation: 48310

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.

Upvotes: 2

Related Questions