网站建设中,中国大陆地区请使用VPN访问,欢迎提建议,关注LSKR Mastodon

C语言链式赋值解析

本文解释了链式赋值写的的原理的原因,以及正确性。

在 C 语言中:

 a = b = c;

的执行顺序(有也就是符号优先级)是:

  1. 先执行 b = c

  2. 然后执行 a = (b = c) 的结果(也就是 c

所以:

 a = b = c;

等价于:

 b = c;
 a = c;

同样适用于结构体成员:

 x.y = x.z = something;

延伸一点点:

为什么链式赋值可以这样做?

因为赋值表达式本身会返回被赋的值(即右边的值),所以你可以把它继续赋给另一个变量。

总结:

表达式:

 (head)->next = (head)->prev = head;

等价于:

 (head)->prev = head;
 (head)->next = head;

作用是:把链表头节点的 next 和 prev 都初始化为指向自己,构建一个空的循环双向链表。




إرسال تعليق

Cookie Consent
我们使用 Cookie 来了解您如何使用我们的网站并提升您的体验。这包括个性化内容和广告。
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.