c中的空指针是什么 [英] what is a null pointer in c
本文介绍了c中的空指针是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include int main(void){结构体 findEntry{整数值;结构体入口 *next;};结构体条目 n1、n2、n3;struct entry *list_pointer = &n1;n1.value = 100;n1.next = &n2;n2.value = 200;n2.next = &n2;n3.value = 300;n3.next = (stuct entry *) 0;while (list_pointer != (struct entry *)0) {printf("%i\n", list_pointer->value);list_pointer = list_pointer->next;}返回0;}
我不明白语法 (struct entry *) 0
在这里是什么意思.在我正在阅读的书中,它说它是一个空指针.我尝试在网上查找,但我不知道该输入什么.我得到的NULL 指针"的 Google 搜索结果与我预期的不同.
解决方案
任何值为 0 的指针类型都称为空指针.以下是 C 标准 §6.3.2.3 的解释:
<块引用>- 值为 0 的整数常量表达式,或这样的表达式强制转换为 void * 类型,称为空指针常量.如果一个空指针常量被转换为指针类型,结果指针,称为空指针,保证比较不等于指向任何对象或函数的指针
#include <stdio.h>
int main(void)
{
struct findEntry
{
int value;
struct entry *next;
};
struct entry n1, n2, n3;
struct entry *list_pointer = &n1;
n1.value = 100;
n1.next = &n2;
n2.value = 200;
n2.next = &n2;
n3.value = 300;
n3.next = (stuct entry *) 0;
while (list_pointer != (struct entry *)0) {
printf("%i\n", list_pointer->value);
list_pointer = list_pointer->next;
}
return 0;
}
I don't understand what the syntax (struct entry *) 0
, means here. In the book I am reading it says it is a null pointer. I tried looking online but I didn't know exactly what to type. The Google search results I got for 'NULL pointer' were different from what I expected.
解决方案
Any pointer type with the value 0 is called a null pointer. Here is the explanation from the C standard §6.3.2.3:
- An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function
这篇关于c中的空指针是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文