Home > Mobile >  Errors when passing pointer to structs c
Errors when passing pointer to structs c

Time:08-14

I am a beginner in C , so I'm sure the error is from my misunderstanding of pointers.

I was making a Poker game, where card is a struct with the suit and the value, and each player's hand is filled with cards, and the middle is filled with cards. I passed references of the arrays to a function by reference, but I think something is wrong with how I set up the pointers to the arrays.

Here is a simplified version of my code:

#include <iostream>
enum suit {hearts, diamonds, spades, clubs};
enum value {two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};

struct card 
{
    value v;
    suit s;
};
static const int num_players = 4;

void get_winner(const card ** hands[num_players][2], const card * mid_cards[5])
{
    for (int i = 0; i < 5; i  ) {
        std::cout << "Middle card " << i   1 << ": " << * mid_cards[i][0] << ", " << * mid_cards[i][1] << "\n";
    }

    for (int i = 0; i < num_players; i  ) {
        std::cout << "Player " << i   1 << "hand: " << * hands[i][0] << ", " << * hands[i][1] << "\n";
    }
}

int main()
{
    card hands[num_players][2];
    card mid_cards[5];
    // fill hands and mid_cards with card structs
    hands[0][0] = card {jack, hearts};
    hands[0][1] = card {nine, spades};
    hands[1][0] = card {ace, clubs};
    hands[1][1] = card {ace, hearts};
    hands[2][0] = card {three, diamonds};
    hands[2][1] = card {seven, spades};
    hands[3][0] = card {eight, diamonds};
    hands[3][1] = card {nine, clubs};
    mid_cards[0] = card {five, clubs};
    mid_cards[1] = card {king, hearts};
    mid_cards[2] = card {queen, spades};
    mid_cards[3] = card {two, clubs};
    mid_cards[4] = card {ace, hearts};
    get_winner(&hands, &mid_cards);
}

Here are the error messages:

main.cpp:17:51: error: indirection requires pointer operand
      ('const card' invalid)
  ...<< "Middle card " << i   1 << ": " << * mid_cards[i][0] << ", " << * mid...
                                           ^ ~~~~~~~~~~~~~~~
main.cpp:17:80: error: indirection requires pointer operand
      ('const card' invalid)
  ...i   1 << ": " << * mid_cards[i][0] << ", " << * mid_cards[i][1] << "\n";
                                                   ^ ~~~~~~~~~~~~~~~
main.cpp:42:2: error: no matching function for call to 'get_winner'
        get_winner(&hands, &mid_cards);
        ^~~~~~~~~~
main.cpp:14:6: note: candidate function not viable: no known conversion from
      'card (*)[4][2]' to 'const card **(*)[2]' for 1st argument
void get_winner(const card ** hands[num_players][2], const card * mid_cards[5])
     ^
3 errors generated.

CodePudding user response:

In your function's parameters, you are using the wrong syntax to accept the arrays by pointer. And, you are declaring the wrong element type for the arrays (you claim card* pointers, but the arrays actual hold card objects instead). And, your function is treating the mid_cards parameter like it is a pointer to a 2-dimentional array when the actual array in main() is 1-dimensional instead.

Also, you do not have an operator<< defined for your card struct, but you are trying to print card values to cout.

Try this instead:

#include <iostream>

enum suit {hearts, diamonds, spades, clubs};
enum value {two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};

struct card 
{
    value v;
    suit s;
};
static const int num_players = 4;

static const char* suits[] = {"Hearts", "Diamonds", "Spades", "Clubs"};
static const char* values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};

std::ostream& operator<<(std::ostream &out, const card &c)
{
    out << values[c.v] << " of " << suits[c.s];
    return out;
}

void get_winner(const card (*hands)[num_players][2], const card (*mid_cards)[5])
{
    for (int i = 0; i < 5; i  ) {
        std::cout << "Middle card " << i   1 << ": " << (*mid_cards)[i] << "\n";
    }

    for (int i = 0; i < num_players; i  ) {
        std::cout << "Player " << i   1 << "hand: " << (*hands)[i][0] << ", " << (*hands)[i][1] << "\n";
    }
}

int main()
{
    card hands[num_players][2];
    card mid_cards[5];
    // fill hands and mid_cards with card structs
    hands[0][0] = card {jack, hearts};
    hands[0][1] = card {nine, spades};
    hands[1][0] = card {ace, clubs};
    hands[1][1] = card {ace, hearts};
    hands[2][0] = card {three, diamonds};
    hands[2][1] = card {seven, spades};
    hands[3][0] = card {eight, diamonds};
    hands[3][1] = card {nine, clubs};
    mid_cards[0] = card {five, clubs};
    mid_cards[1] = card {king, hearts};
    mid_cards[2] = card {queen, spades};
    mid_cards[3] = card {two, clubs};
    mid_cards[4] = card {ace, hearts};
    get_winner(&hands, &mid_cards);
}

Online Demo

Alternatively, pass the arrays by reference instead of pointer, eg:

#include <iostream>

enum suit {hearts, diamonds, spades, clubs};
enum value {two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};

struct card 
{
    value v;
    suit s;
};
static const int num_players = 4;

static const char* suits[] = {"Hearts", "Diamonds", "Spades", "Clubs"};
static const char* values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};

std::ostream& operator<<(std::ostream &out, const card &c)
{
    out << values[c.v] << " of " << suits[c.s];
    return out;
}

void get_winner(const card (&hands)[num_players][2], const card (&mid_cards)[5])
{
    for (int i = 0; i < 5; i  ) {
        std::cout << "Middle card " << i   1 << ": " << mid_cards[i] << "\n";
    }

    for (int i = 0; i < num_players; i  ) {
        std::cout << "Player " << i   1 << "hand: " << hands[i][0] << ", " << hands[i][1] << "\n";
    }
}

int main()
{
    card hands[num_players][2];
    card mid_cards[5];
    // fill hands and mid_cards with card structs
    hands[0][0] = card {jack, hearts};
    hands[0][1] = card {nine, spades};
    hands[1][0] = card {ace, clubs};
    hands[1][1] = card {ace, hearts};
    hands[2][0] = card {three, diamonds};
    hands[2][1] = card {seven, spades};
    hands[3][0] = card {eight, diamonds};
    hands[3][1] = card {nine, clubs};
    mid_cards[0] = card {five, clubs};
    mid_cards[1] = card {king, hearts};
    mid_cards[2] = card {queen, spades};
    mid_cards[3] = card {two, clubs};
    mid_cards[4] = card {ace, hearts};
    get_winner(hands, mid_cards);
}

Online Demo

  • Related