《c陷阱與缺陷》第二章,語法陷阱

2.1 函數聲明

float *g();  // indicate a function returning a pointer to a float

float (*h)();//indicate a pointer to a function returning a float

*h是一個float類型的函數,h是指向這個函數的指針

float是類型聲明,說明後面的類型是float類型的

g()中,()表示函數調用

類型轉換符((*h)())表示“指向返回浮點類型的函數的指針”類型轉換符

 

Q:調用首地址爲0位置的子例程

(*0)();但是*必須要一個指向函數的指針作爲操作數,所以增加格式轉換,

擴充爲(*(void (*)())0)();

eg: 

       void(*)()             // indicates a pointer to a function returning nothing

    ((void(*)())0)        //change the type of 0 from integer to a pointer

(*((void(*)())0))      //obtain the data pointed by the pointer, that is the  subroutine in location 0

(*((void(*)())0))();   // call the subroutine

 

3) define a type for a function pointer

 typedef float(*ptr)();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章