卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章17169本站已运行3325

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

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

空指针是指没有指向任何东西的指针。

空指针的一些用途:

b) 当指针变量尚未分配任何有效的内存地址时,用于初始化指针变量。

b) 当我们不想传递任何有效的内存地址时,将空指针传递给函数参数。

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 <stdio.h>
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.
卓越飞翔博客
上一篇: 掌握 wp-config.php 文件 - 11 个基本技巧
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏