C語言指針函數和函數指針的區別


指針函數(本質是一個函數):

    int * GetDate(int wk,int dy); main() { int wk,dy; do { printf(Enter week(1-5)day(1-7)\n); scanf(%d%d,&wk,&dy); } while(wk<1||wk>5||dy<1||dy>7); printf(%d\n,*GetDate(wk,dy)); } int * GetDate(int wk,int dy) { static int calendar[5][7]= { {1,2,3,4,5,6,7}, {8,9,10,11,12,13,14}, {15,16,17,18,19,20,21}, {22,23,24,25,26,27,28}, {29,30,31,-1} }; return &calendar[wk-1][dy-1]; } 程序應該是很好理解的,子函數返回的是數組某元素的地址。輸出的是這個地址里的值。

函數指針(本質是一個指針):

 

    void (*funcp)(); void FileFunc(),EditFunc(); main() { funcp=FileFunc; (*funcp)(); funcp=EditFunc; (*funcp)(); } void FileFunc() { printf(FileFunc\n); } void EditFunc() { printf(EditFunc\n); } 程序輸出為: FileFunc EditFunc

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM