Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare pointer to array of structs in C [duplicate]

Tags:

c

pointers

struct

New to pointers and C and need for my program a pointer for an array of structs and be able to pass this pointer to a function.

What is the correct way to declare a pointer of struct array type and what should be my function parameter that can take such 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 the pointers are different types but I cant seem to figure out how to set it correctly.

I'll appreciate if someone can *pointer me in the right direction.

like image 823
user2300867 Avatar asked Nov 08 '16 01:11

user2300867


2 Answers

An array degrades into a raw pointer to the first array element. So you can do something more like this instead:

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

Alternatively:

#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 card array, and then you can create variables and pointers of that 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...
    }
}
like image 194
Remy Lebeau Avatar answered Oct 10 '22 17:10

Remy Lebeau


Maybe what you want is to do this:

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

and this:

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

}

What you were doing was passing a pointer to an array of struct variables in the main, BUT, the function was set to receive an array of pointers to struct variables and not a pointer to an array of struct variables! Now the type mismatch would not occur and thus, no warning.

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

like image 38
skrtbhtngr Avatar answered Oct 10 '22 17:10

skrtbhtngr