ในภาษาการเขียนโปรแกรม C ตัวชี้ไปยังตัวชี้หรือตัวชี้คู่เป็นตัวแปรที่เก็บที่อยู่ของตัวชี้อื่น
ประกาศ
รับด้านล่างเป็นการประกาศสำหรับตัวชี้ไปยังตัวชี้ -
datatype ** pointer_name;
ตัวอย่างเช่น int **p;
ที่นี่ p เป็นตัวชี้ไปยังตัวชี้
การเริ่มต้น
'&' ใช้สำหรับการเริ่มต้น
ตัวอย่างเช่น
int a = 10; int *p; int **q; p = &a;
การเข้าถึง
ตัวดำเนินการทางอ้อม (*) ใช้สำหรับการเข้าถึง
โปรแกรมตัวอย่าง
ต่อไปนี้เป็นโปรแกรม C สำหรับตัวชี้คู่ -
#include<stdio.h>
main ( ){
int a = 10;
int *p;
int **q;
p = &a;
q = &p;
printf("a =%d ",a);
printf(" a value through pointer = %d", *p);
printf(" a value through pointer to pointer = %d", **q);
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
a=10 a value through pointer = 10 a value through pointer to pointer = 10
ตัวอย่าง
ตอนนี้ให้พิจารณาโปรแกรม C อื่นที่แสดงความสัมพันธ์ระหว่างตัวชี้ไปยังตัวชี้
#include<stdio.h>
void main(){
//Declaring variables and pointers//
int a=10;
int *p;
p=&a;
int **q;
q=&p;
//Printing required O/p//
printf("Value of a is %d\n",a);//10//
printf("Address location of a is %d\n",p);//address of a//
printf("Value of p which is address location of a is %d\n",*p);//10//
printf("Address location of p is %d\n",q);//address of p//
printf("Value at address location q(which is address location of p) is %d\n",*q);//address of a//
printf("Value at address location p(which is address location of a) is %d\n",**q);//10//
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
Value of a is 10 Address location of a is 6422036 Value of p which is address location of a is 10 Address location of p is 6422024 Value at address location q(which is address location of p) is 6422036 Value at address location p(which is address location of a) is 10