跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

为什么在C/C++中使用零地址作为空指针?

空指针是指没有指向任何东西的指针。 空指针的一些用途: b) 当指针变量尚未分配任何有效的内存地址时,用于初始化指针变量。 b) 当我们不想传递任何有效的内存地址时,将空指针传递给函数参数。 立即学习 “ C++免费学习笔记(深入) ”; c) 在访问任何指针变量之前检查空指针。这样,我们可以在与指针相关的代码中进行错误处理,例如仅在指针变量不为空时才解引用指针变量。 在C++中,如果我们将0赋值给任何指针,那么指针指向NULL。 语法
Float *p = 0 //initializing the pointer as NULL.
算法
Begin. Declare a pointer p of the integer datatype. Initialize *p= NULL. Print “The value of pointer is”. Print the value of the pointer p. End.
Example: 现场演示
#include int main() { int *p= NULL;//initialize the pointer as null. printf("The value of pointer is %u",p); return 0; }
输出
The value of pointer is 0.

相关文章