ตัวชี้คือตัวแปรที่มีค่าเป็นที่อยู่ของตัวแปรหรือบล็อกหน่วยความจำอื่น เช่น ที่อยู่ตรงของตำแหน่งหน่วยความจำ เช่นเดียวกับตัวแปรหรือค่าคงที่ใดๆ คุณต้องประกาศตัวชี้ก่อนที่จะใช้เพื่อเก็บตัวแปรหรือบล็อกแอดเดรส
ไวยากรณ์
Datatype *variable_name
อัลกอริทึม
Begin. Define a function show. Declare a variable x of the integer datatype. Print the value of varisble x. Declare a pointer p of the integer datatype. Define p as the pointer to the address of show() function. Initialize value to p pointer. End.
นี่เป็นตัวอย่างง่ายๆ ในภาษา C เพื่อให้เข้าใจแนวคิดของตัวชี้ไปยังฟังก์ชัน
#include
void show(int x)
{
printf("Value of x is %d\n", x);
}
int main()
{
void (*p)(int); // declaring a pointer
p = &show; // p is the pointer to the show()
(*p)(7); //initializing values.
return 0;
} ผลลัพธ์
Value of x is 7.