C语言的一些奇技淫巧(1)嵌套语句

1.嵌套语句/复合语句

1.1 例子 for(i=0; i<3 && ({j=1;TRUE;}); i++)

此句中, i<3 && ({j=1;TRUE;}); 是一个语句,
后面 ({j=1;TRUE;}) 是一个复合语句
这句无论j=1 怎么变,都是最后一句 TRUE 为真。
所以真正的还是前面i<3 成立即可

2. list_for_each_safe() 源码例子

/**
 * list_for_each_safe	-	iterate over a list safe against removal of list entry
* @pos:	the &struct list_head to use as a loop counter.
* @n:		another &struct list_head to use as temporary storage
* @head:	the head for your list.
*/
源码:
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
	pos = n, n = pos->next)

修改如下:
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next; (pos != (head) &&  ( { n = pos->next;TRUE; } ) ); \
	pos = n, n = pos->next)
  1. 重点: (pos != (head) && ( { n = pos->next;TRUE; } )
    这是一个复合语句,后面总是true,为了让本次循环只要前面true就执行下去
    遍历时用n 接住下一个节点,防止pos 删除操作导致找不到节点了

参考

https://blog.csdn.net/choice_jj/article/details/7496732

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值