Pointer to Pointer (Double Pointer)
ตัวแปรที่เก็บ address ของ pointer อีกตัว
Syntax
datatype **pointer_name;
ตัวอย่าง
int a = 10;
int *p = &a;
int **q = &p;
printf("%d", **q); // 10
โปรแกรม Pointer to Pointer
#include <stdio.h>
main() {
int A = 10;
int *p = &A;
int **q = &p;
printf("A = %d\n", A);
printf("*p = %d\n", *p);
printf("**q = %d\n", **q);
}
ผลลัพธ์
A = 10
*p = 10
**q = 10
Void Pointer
Pointer ที่ชี้ไปยัง data type ใดๆ ได้ (generic pointer)
Syntax
void *ptr;
การเข้าถึง: ต้อง type cast
int i = 10;
void *vp = &i;
printf("%d", *((int*)vp)); // cast เป็น int*
โปรแกรม Void Pointer
#include <stdio.h>
main() {
int i = 10;
float f = 5.34;
void *vp;
vp = &i;
printf("i = %d\n", *((int*)vp));
vp = &f;
printf("f = %f\n", *((float*)vp));
}
ผลลัพธ์
i = 10
f = 5.34