提问人:Zzz 提问时间:10/26/2023 最后编辑:BarmarZzz 更新时间:11/2/2023 访问量:54
“->”的类型参数无效(具有未签名的 int)错误 [重复]
Invalid type argument of '->' (have unsigned int) error [duplicate]
问:
我在 c 中将宏函数定义为
#define PortA_Clk_En() RCC->AHB1ENR |= (1 << 0)
但是当我在程序中调用宏函数时,它会给出以下错误
“->”的类型参数无效(具有无符号 int)
RCC 类型转换为
#define base_addr 0x20097654
#define RCC (Reg_t*)base_addr
在哪里Reg_t
typedef struct{
uint32_t AHB1ENR;
}Reg_t;
我不明白问题出在哪里。
答:
4赞
dbush
10/26/2023
#1
这:
RCC->AHB1ENR
将扩展到此:
(Reg_t*)base_addr->AHB1ENR
由于运算符的优先级高于类型转换运算符,因此它的解析方式如下:->
(Reg_t*)(base_addr->AHB1ENR)
由于它不是指向结构或联合的指针,因此会出现错误。base_addr
您需要在括号中添加括号,以便按照您想要的方式对事物进行分组:#define
#define RCC ((Reg_t*)base_addr)
1赞
CiaPan
10/26/2023
#2
用
#define PortA_Clk_En() (RCC)->AHB1ENR |= (1 << 0)
或者,甚至更好(THX用户Barmar!
#define PortA_Clk_En() ((RCC)->AHB1ENR |= (1 << 0))
强制执行必要的语义
((Reg_t*)base_addr)->AHB1ENR |= (1 << 0)
或全括号
(((Reg_t*)base_addr)->AHB1ENR |= (1 << 0))
而不是
(Reg_t*)base_addr->AHB1ENR |= (1 << 0)
就像现在一样。
评论
1赞
Barmar
10/26/2023
还要在整个扩展包周围加上括号。
0赞
CiaPan
11/2/2023
@Barmar补充道。谢谢。
1赞
0___________
10/26/2023
#3
你错过了括号
#define PortA_Clk_En() RCC->AHB1ENR |= (1 << 0)
#define base_addr 0x20097654
#define RCC ((Reg_t*)base_addr)
typedef struct{
uint32_t AHB1ENR;
}Reg_t;
评论