Randomblue
Randomblue

Reputation: 116413

Issues with struct

I am trying to call a library function with signature

void GPIO_Init(GPIO_InitTypeDef* GPIO_InitStruct)

where GPIO_InitTypeDef is a typedef struct.

I have tried doing the following:

GPIO_InitTypeDef NE1 = {
    7, GPIO_Mode_AF, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_UP
};
GPIO_Init(NE1);

but I get a compiler error

error: incompatible type for argument 1 of 'GPIO_Init' expected 'struct GPIO_InitTypeDef *' but argument is of type 'GPIO_InitTypeDef'

I have also tried using the struct keyword:

struct GPIO_InitTypeDef NE1 = {
    7, GPIO_Mode_AF, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_UP
};
GPIO_Init(NE1);

but them I get the compiler error

error: storage size of 'NE1' isn't known

What am I doing wrong, and what is the proper way to call GPIO_Init?

Upvotes: 0

Views: 496

Answers (1)

paxdiablo
paxdiablo

Reputation: 882666

You need to use:

GPIO_Init (&NE1); // <- Note the '&' indicating pointer-to

That function expects a pointer to a GPIO_InitStruct structure, as indicated with:

void GPIO_Init (GPIO_InitTypeDef * GPIO_InitStruct)
//                               ^
//                            pointer

But your NE1 variable is an actual structure, so you have to use & to get the pointer to it, so you can pass that.

Because you're trying to pass the structure instead of a pointer to a structure, that's what's causing your incompatible type error.

Upvotes: 1

Related Questions