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 = #
//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語言中將函數指針作為形參