web-dev-qa-db-fra.com

Comment déclarer un pointeur sur un tableau de structures en C

Nouveau pour les pointeurs et le C et nécessite pour mon programme un pointeur pour un tableau de structures et pouvoir passer ce pointeur à une fonction. 

Quelle est la bonne façon de déclarer un pointeur de type struct array et quel doit être le paramètre de fonction qui peut prendre un tel pointeur?

Ceci est ma tentative:

#define HAND_SIZE 5

struct Card {
    char suit;
    char face;
};

void printHandResults(struct Card *hand[HAND_SIZE]);

int main(void)
{
    struct Card hand[HAND_SIZE];
    struct Card (*handPtr)[HAND_SIZE]; //Correct way to declare?
    handPtr = &hand; 
    ...
    printHandResults(handPtr);

}
void printHandResults(struct Card *hand[HAND_SIZE]) {
...
}

Et voici l'avertissement que je reçois:

warning: incompatible pointer types passing 'struct Card (*)[5]' to parameter of type 'struct Card **' [-Wincompatible-pointer-types]

Je comprends que les pointeurs sont de types différents mais je n'arrive pas à comprendre comment le régler correctement.

J'apprécierai si quelqu'un peut * me diriger dans la bonne direction. 

7
user2300867

Peut-être que ce que vous voulez, c'est faire ceci:

void printHandResults(struct Card (*hand)[]);

et ça:

void printHandResults(struct Card (*hand)[]) {

}

Ce que vous faisiez, c’était de passer un pointeur à un tableau de variables de structure dans le menu principal, MAIS, la fonction était configurée pour recevoir un tableau de pointeurs vers des variables de structure et non un pointeur vers un tableau de variables de structure ! Désormais, l'inadéquation des types ne se produirait pas et donc, aucun avertissement.

Notez que [], les crochets (crochets) ont une priorité supérieure à celle de l'opérateur de déréférencement (unaire) *; nous aurions donc besoin d'un ensemble de parenthèses () entourant le nom du tableau et de l'opérateur * pour garantir le contenu de la discussion!

6
skrtbhtngr

Un tableau se dégrade en un pointeur brut vers le premier élément du tableau. Vous pouvez donc faire quelque chose de plus semblable à ceci:

#define HAND_SIZE 5

struct Card {
    char suit;
    char face;
};

void printHandResults(struct Card *hand);

int main(void)
{
    struct Card hand[HAND_SIZE];
    ...
    printHandResults(hand);
}

void printHandResults(struct Card *hand)
{
    for (int i = 0; i < HAND_SIZE; ++i)
    {
        // print hand[i].suit and hand[i].face as needed...
    }
}

Alternativement:

#define HAND_SIZE 5

struct Card {
    char suit;
    char face;
};

void printHandResults(struct Card *hand, int numInHand);

int main(void)
{
    struct Card hand[HAND_SIZE];
    ...
    printHandResults(hand, HAND_SIZE);
}

void printHandResults(struct Card *hand, int numInHand)
{
    for (int i = 0; i < numInHand; ++i)
    {
        // print hand[i].suit and hand[i].face as needed...
    }
}

Vous pouvez également créer une nouvelle typedef pour le tableau de cartes, puis créer des variables et des pointeurs de ce type:

#define HAND_SIZE 5

struct Card {
    char suit;
    char face;
};

typedef struct Card Hand[HAND_SIZE];

void printHandResults(Hand *hand);

int main(void)
{
    Hand hand;
    ...
    printHandResults(&hand);
}

void printHandResults(Hand *hand)
{
    for (int i = 0; i < HAND_SIZE; ++i)
    {
        // print hand[i].suit and hand[i].face as needed...
    }
}
5
Remy Lebeau

Plus facile:

#define HAND_SIZE 5

typedef struct Cards{
   char suit;
   char face;
}card_t;

void printHandResults( card_t*);

int main(void)
{
   card_t hand[HAND_SIZE];
   card_t* p_hand = hand;
...
   printHandResults(p_hand);

}
void printHandResults( card_t *hand)
{
 // Here you must play with pointer's arithmetic 
...
}