Search code examples
cstructcasting

Invalid type argument of '->' (have unsigned int) error


I defined a macro function in c as

#define PortA_Clk_En()     RCC->AHB1ENR |= (1 << 0)

But when I call the macro function inside the program it gives the following error

Invalid type argument of '->' (have unsigned int)

RCC is type casted as

#define base_addr    0x20097654
#define RCC     (Reg_t*)base_addr

Where Reg_t is

typedef struct{
    uint32_t AHB1ENR;
}Reg_t;

I don't understand what the problem is.


Solution

  • This:

    RCC->AHB1ENR
    

    Will expand to this:

    (Reg_t*)base_addr->AHB1ENR
    

    And since the -> operator has higher precedence than the typecast operator, it parses as this:

    (Reg_t*)(base_addr->AHB1ENR)
    

    And since base_addr isn't a pointer to a struct or union, you get the error.

    You need to add parenthesis to your #define so things are grouped the way you want:

    #define RCC     ((Reg_t*)base_addr)