c - C:围绕没有其他操作数的变量的括号
问题描述
我正在研究 FreeRTOS 的源代码。我找到了这个片段: https ://github.com/TheKK/myFreeRTOS/blob/master/include/list.h#L268
#define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList ) \
{ \
List_t * const pxConstList = ( pxList ); \
/* Increment the index to the next item and return the item, ensuring */ \
/* we don't return the marker used at the end of the list. */ \
( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
if( ( void * ) ( pxConstList )->pxIndex == ( void * ) &( ( pxConstList )->xListEnd ) ) \
{ \
( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
} \
( pxTCB ) = ( pxConstList )->pxIndex->pvOwner; \
}
我知道这( pxConstList )->pxIndex
意味着访问指针变量 pxConstList 的数据。但我想知道 是什么意思surround a variable with parenthesis only
?
那是:
List_t * const pxConstList = ( pxList );
~~~~~~~~~~
和
( pxTCB ) = ( pxConstList )->pxIndex->pvOwner;
~~~~~~~~~~
谢谢。
解决方案
这些变量实际上是宏的参数。括号是必要的,因为一个参数可能是一个表达式,如果没有它们,它就不能正确地适应上下文。
例如考虑这个宏:
#define SQUARE(x) x*x
如果你这样调用这个宏:
SQUARE(4+5)
你会得到:
4+5*4+5
因为*
具有比 更高的优先级+
,这将评估为 29 而不是预期的 81。如果宏定义如下:
#define SQUARE(x) (x)*(x)
然后上面将扩展为:
(4+5)*(4+5)
这将评估为 81。
总而言之,建议始终将宏参数括起来以避免这些类型的问题。
推荐阅读
- sqlite - 烧瓶应用程序中的 SQL Alchemy Query 用于多对多关系
- spring - IllegalStateException:无法将“StandardMultipartHttpServletRequest$StandardMultipartFile”类型的值转换为所需的“java.lang.String”类型
- firebase - 错误:没有为类“对象”定义吸气剂“文档”。- “对象”来自“飞镖:核心”
- c++ - boost::qi 按最后一个顺序项评估结果
- angular - 使用 Angular。当我单击链接时,我想打开新页面来显示,而不是显示在链接下方
- mysql - Django:如何通过 ORM 或 Mysql 过滤属性?
- activemq-artemis - ActiveMQ Artemis - max-delivery-attempts 超过 7
- java - 线程“JavaFX 应用程序线程”中的异常 java.lang.IllegalArgumentException:此 ParagraphText 不是给定形状的父级
- c++ - 按规则对结构的 std::list 进行排序
- business-objects - SAP BO - 将工作时间与已用时间隔离开来