Null pointer คือ pointer ที่ชี้ไม่ไปที่ไหน
การใช้งาน null pointer:
a) Initialize pointer variable เมื่อ pointer ยังไม่ได้ assign valid memory address
b) ส่ง null pointer ให้ function argument เมื่อไม่ต้องการส่ง valid memory address
c) ตรวจสอบ null pointer ก่อน access pointer variable เพื่อ error handling เช่น dereference เฉพาะเมื่อไม่ใช่ NULL
Algorithm
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.
ตัวอย่าง
#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.