1 /* Exported types ------------------------------------------------------------*/ 2 /* Exported constants --------------------------------------------------------*/
3 /* Uncomment the line below to expanse the \"assert_param\" macro in the 4 Standard Peripheral Library drivers code */ 5 /* #define USE_FULL_ASSERT 1 */ 6
7 /* Exported macro ------------------------------------------------------------*/ 8 #ifdef USE_FULL_ASSERT 9 10 /**
11 * @brief The assert_param macro is used for function's parameters check.12 * @param expr: If expr is false, it calls assert_failed function which reports 13 * the name of the source file and the source line number of the call 14 * that failed. If expr is true, it returns no value.15 * @retval None16 */
17 #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))18 /* Exported functions ------------------------------------------------------- */19 void assert_failed(uint8_t* file, uint32_t line);20 #else
21 #define assert_param(expr) ((void)0)22 #endif /* USE_FULL_ASSERT */
看到⼀个宏, 它⼤概是这样的:
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((u8 *)__FILE__, __LINE__)) 代码的含意简单, 关键是那个 (void)0 的⽤法, 我还是第⼀次见到(别笑). 我⽤ void 的时候, 有两种情况:
1.放到函数前⾯, 强调函数没有返回值, 也就是说函数不能作右值 如: void fun(int x);
2.放到函数形参⾥⾯, 强调函数⽆任何参数 如: int fun(void); 还有⼀种⽤法是:
#define NULL ((void*)0)
当然, 这就是NULL空指针的定义⽅式(在 stdlib.h ⾥⾯).
可, 上⾯宏的 (void)0 , ⼀开始确实让我觉得有点奇怪, 不知道⼲嘛的, 平静下来, 想了想.
原来, 宏⾥⾯这样⽤的⽬的是防⽌该宏被⽤作右值, (void)0 本⾝也不能作右值, 因为 void ⾮实际的类型!
因篇幅问题不能全部显示,请点此查看更多更全内容