c语言中将函数指针作为形参_在C中将有效指针作为NULL指针

本文介绍了如何在C语言中将函数指针作为形参使用,并展示了如何通过赋值0将一个有效指针转换为NULL指针的示例。
摘要由CSDN通过智能技术生成

c语言中将函数指针作为形参

Prerequisite: An Example of Null pointer in C

先决条件: C中的空指针示例

Any pointer that contains a valid memory address can be made as a NULL pointer by assigning 0.

通过分配0,可以将包含有效内存地址的任何指针设置为NULL指针

Example:

例:

Here, firstly ptr is initialized by the address of num, so it is not a NULL pointer, after that, we are assigning 0 to the ptr, and then it will become a NULL pointer.

在这里,首先ptr由num的地址初始化,因此它不是NULL指针,此后,我们为ptr分配0 ,然后它将成为NULL指针。

Program:

程序:

#include <stdio.h>

int main(void) {
	int num = 10;
	int *ptr = &num;
	
	//we can also check with 0 instesd of NULL 
	if(ptr == NULL)
		printf("ptr: NULL\n");
	else
		printf("ptr: NOT NULL\n");

	//assigning 0 
	ptr = 0;
	if(ptr == NULL)
		printf("ptr: NULL\n");
	else
		printf("ptr: NOT NULL\n");

	return 0;
}

Output

输出量

ptr: NOT NULL
ptr: NULL


翻译自: https://www.includehelp.com/c-programs/making-a-valid-pointer-as-null-pointer-in-c.aspx

c语言中将函数指针作为形参

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值