如何在 C 中宣告指向函式的指標?
指標是一個變數,其值是另一個變數或記憶體塊的地址,即記憶體位置的直接地址。與任何變數或常量一樣,在使用指標儲存任何變數或塊地址之前,必須對其進行宣告。
語法
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.
廣告