c语言指针示例_C中的空指针示例

c语言指针示例

空指针 (NULL pointer)

The word "NULL" is a constant in C language and its value is 0. In case with the pointers - if any pointer does not contain a valid memory address or any pointer is uninitialized, known as "NULL pointer". We can also assign 0 (or NULL) to make a pointer as "NULL pointer".

单词“ NULL”是C语言中的常量,其值为0 。 如果使用指针-如果任何指针不包含有效的内存地址或任何指针未初始化,则称为“ NULL指针” 。 我们还可以将0 (或NULL )分配为“ NULL指针”

Example:

例:

In this example, there are 3 integer pointers ptr1, ptr2 and ptr3. ptr1 is initialized with the address of the integer variable num, thus ptr1 contains a valid memory address. ptr2 is uninitialized and ptr3 assigned 0. Thus, ptr2 and ptr3 are the NULL pointers.

在此示例中,有3个整数指针ptr1 , ptr2和ptr3 。 ptr1用整数变量num的地址初始化,因此ptr1包含有效的内存地址。 ptr2未初始化,并且ptr3分配为0。因此, ptr2和ptr3是NULL指针

Program:

程序:

#include <stdio.h>

int main(void) {
	int num = 10;

	int *ptr1 = &num;
	int *ptr2;
	int *ptr3=0;

	if(ptr1 == 0)
		printf("ptr1: NULL\n");
	else
		printf("ptr1: NOT NULL\n");

	if(ptr2 == 0)
		printf("ptr2: NULL\n");
	else
		printf("ptr2: NOT NULL\n");

	if(ptr3 == 0)
		printf("ptr3: NULL\n");
	else
		printf("ptr3: NOT NULL\n");

	return 0;
}

Output

输出量

ptr1: NOT NULL 
ptr2: NULL 
ptr3: NULL 


翻译自: https://www.includehelp.com/c-programs/an-example-of-null-pointer-in-c.aspx

c语言指针示例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值