我有一个关于 STM32 微控制器的 C 语言类型转换的问题。现在我正在尝试编写自己的库来控制微控制器的外围设备。但我不明白为什么以及何时应该使用像 (uint32_t) 这样的类型转换。
例如,对于 GPIO 配置寄存器,我看到了下一个示例:
#define INPUT_ANALOG ((uint32_t) 0x00)
#define INPUT_FLOATING ((uint32_t) 0x01)
#define INPUT_PUP_PDOWN ((uint32_t) 0x02)
Run Code Online (Sandbox Code Playgroud)
此信息来自数据表(下面附有打印屏幕)。在这里我不明白为什么使用 typecast (uint32_t) 而不仅仅是 0x00 或 0。
我可以将下一个代码用于相同的目的吗?
typedef enum{
INPUT_ANALOG = 0,
IN_FLOATING,
IN_PULL_UP_DOWN,
}PinConfInput_t;
Run Code Online (Sandbox Code Playgroud)
我应该对此代码进行类型转换吗?你能给我推荐一些关于 typecast 的学习资料吗?谢谢
我有C语言中的代码示例,它模拟了虚函数Shape_area()。
我不懂一行代码: return (*me->vptr->area)(me);
为什么在“我”之前使用“ *”?
你能告诉我那部分吗?谢谢
/*shape.h*/
struct ShapeVtbl; /* forward declaration */
typedef struct {
struct ShapeVtbl const *vptr; /* <== Shape's Virtual Pointer */
int16_t x; /* x-coordinate of Shape's position */
int16_t y; /* y-coordinate of Shape's position */
} Shape;
/* Shape's virtual table */
struct ShapeVtbl {
uint32_t (*area)(Shape const * const me);
};
/* Shape's operations (Shape's interface)... */
void Shape_ctor(Shape * const me, int16_t x, int16_t y);
void Shape_moveBy(Shape * const me, …Run Code Online (Sandbox Code Playgroud)