c - 这个 C 代码片段中的“p?p->next:0”是什么,我该如何运行它?
问题描述
我想检查这个 p?p->next:0 是如何工作的。它与java中的运算符不同吗?替换,“如果”的一个衬里?
例如
x = (5>6)? 10 : 4 ; //should assign x =4
struct node
{
int value;
struct node *next;
};
void rearrange(struct node *list)
{
struct node *p, * q;
int temp;
if ((!list) || !list->next)
return;
p = list;
q = list->next;
while(q)
{
temp = p->value;
p->value = q->value;
q->value = temp;
p = q->next;
q = p?p->next:0;
}
}
//int主要
解决方案
在 C 中,0 是false
,其他一切都是true
。这里使用的三元运算符是
if (p)
{
q = p->next;
}
else
{
q = 0;
}
if(p)
对于 的任何值,哪里都为真p != 0
。在处理指针时,我的偏好是比较NULL
而不是 0, if (p != NULL){ ... }
,但看到使用 0 的情况并不少见。在我工作过的每个系统上,NULL
都是 0,但我不认为这是标准中规定的..?这里有人会知道的。
推荐阅读
- google-bigquery - 使用 insertAll 将数据加载到 BigQuery 时每天出现一次“后端错误”
- node.js - 带有 TypeORM 的 Node.js 将错误的时区插入 Azure SQL 数据库
- azure-data-factory - 使用 .NET SDK 运行和监控 ADF 管道运行
- python - Pygame:从精灵表中显示这些船只及其运动的最佳方式是什么?
- javascript - 将 Array 中的每个元素与不同的值相乘?代码不起作用
- python - 如何从 Python 中的现在分词或其他变体中找到单词的词根?
- javascript - 如何使用 moment.js 从当前月份获取前几个月
- r - 多变量观察绘图工具提示
- python-3.x - 即使对象是 None 类型也返回属性
- python - Django查询列表到模板 - 按类别显示项目