首页 > 解决方案 > Type casting to pointer, precedence with addition

问题描述

#define GPIO_BASE 0x40020000
#define GPIO_MEM_SIZE 0x0400

Those are the macro definitions which will be related.

GPIO_Type is just a typedef struct I did.

volatile GPIO_Type *GPIOB = (GPIO_Type*) (GPIO_BASE + GPIO_MEM_SIZE);

This is fine, I am casting summation of two integers to the type of pointer I am defining.

If I do it as,

volatile GPIO_Type *GPIOB = GPIO_BASE + GPIO_MEM_SIZE;

Compiler gives a warning about "conversion from integer to pointer without casting". I understand this.

However, when I do,

volatile GPIO_Type *GPIOB = (GPIO_Type*) GPIO_BASE + GPIO_MEM_SIZE;

It(compiler) does not give a warning or error, but I know that type casting has higher precedence than addition, so it((GPIO_Type*)) does not include GPIO_MEM_SIZE, which is an integer.

So, why it is working without any problem? Does the compiler automatically cast GPIO_MEM_SIZE also?

标签: ccastingtype-conversion

解决方案


推荐阅读