How to declare a pointer to an array of structures in C

It is new for pointers and C, and it is necessary that my program points to an array of structures and can pass this pointer to a function.

What is the correct way to declare a pointer to a struct array type and what should be my function parameter that can take such a pointer?

This is my attempt:

#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]) { ... } 

And this is the warning I get:

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

I understand that the pointers are different, but I cannot figure out how to set them correctly.

I would appreciate it if someone could * point me in the right direction.

+6
source share
3 answers

Perhaps you want to do this:

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

and this:

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

What you did was pass a pointer to an array of structural variables basically, BUT, the function was configured to receive an array of pointers to structural variables , not a pointer to an array of structural variables ! Now a type mismatch will not occur and therefore there will be no warnings.

Note that [] , (square) brackets have a higher priority than the (unary) dereference operator * , so we need a set of parentheses () including the name of the array and * to ensure what we are talking about here!

+5
source

The array degrades to the original pointer to the first element of the array. So you can do something more like this:

 #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... } } 

As an alternative:

 #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... } } 

Alternatively, create a new typedef for the map array, and then you can create variables and pointers of this 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... } } 
+4
source

A simpler way:

 #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 arithmetic ... } 
0
source

Source: https://habr.com/ru/post/1012126/


All Articles