ตัวชี้การทำงาน
มันเก็บที่อยู่พื้นฐานของการกำหนดฟังก์ชันในหน่วยความจำ
ประกาศ
datatype (*pointername) ();
ชื่อของฟังก์ชันเองจะระบุที่อยู่พื้นฐานของฟังก์ชัน ดังนั้น การเริ่มต้นทำได้โดยใช้ชื่อฟังก์ชัน
ตัวอย่างเช่น
int (*p) (); p = display; //display () is a function that is defined.
ตัวอย่างที่ 1
เราจะเห็นโปรแกรมสำหรับการเรียกใช้ฟังก์ชันโดยใช้ตัวชี้ไปยังฟังก์ชัน -
#include<stdio.h>
main (){
int (*p) (); //declaring pointer to function
clrscr ();
p = display;
*(p) (); //calling pointer to function
getch ();
}
display (){ //called function present at pointer location
printf(“Hello”);
} ผลลัพธ์
Hello
ตัวอย่างที่ 2
ให้เราพิจารณาอีกโปรแกรมหนึ่งที่อธิบายแนวคิดของพอยน์เตอร์ในการทำงาน -
#include <stdio.h>
void show(int* p){
(*p)++; // add 1 to *p
}
int main(){
int* ptr, a = 20;
ptr = &a;
show(ptr);
printf("%d", *ptr); // 21
return 0;
} ผลลัพธ์
21