C語(yǔ)言的那些小秘密之函數(shù)指針
有注釋前的運(yùn)行結(jié)果為:
本文引用地址:http://www.ex-cimer.com/article/270442.htm
把注釋部分加進(jìn)去的運(yùn)行結(jié)果為:
對(duì)比以上的運(yùn)行結(jié)果可以看出,f=&max語(yǔ)句被執(zhí)行時(shí)的結(jié)果和沒(méi)有被執(zhí)行時(shí)的結(jié)果是一樣的。為什么會(huì)出現(xiàn)這樣的結(jié)果呢?答案是這是編譯器處理的,max本身就是個(gè)地址,它沒(méi)有放到任何變量里,自然沒(méi)有取它的地址一說(shuō)。所以我們可以看看在調(diào)試的過(guò)程中&max的值和max的值是一樣的。調(diào)試代碼如下:
root@ubuntu:/home/shiyan# gdb ss
GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/shiyan/ss...done.
(gdb) list
1 #include
2 int max ( int x, int y){ return x>y?x:y;}
3 int min ( int x, int y){ return x
4 void main ()
5 { int ( *f ) ( int x, int y)=max;
6 //f=&max;
7 printf ( "%d,%dt", max (2,6), (f)(5,4));
8 f=min;
9 printf (" %d,%dt" , min (2,6), (f)(5,4));
10 }
(gdb) b 4
Breakpoint 1 at 0x80483ec: file hanshu.c, line 4.
(gdb) r
Starting program: /home/shiyan/ss
Breakpoint 1, main () at hanshu.c:5
5 { int ( *f ) ( int x, int y)=max;
(gdb) print max
$1 = {int (int, int)} 0x80483c4
(gdb) print f
$2 = (int (*)(int, int)) 0xbffff6c8
(gdb) s
7 printf ( "%d,%dt", max (2,6), (f)(5,4));
(gdb)
max (x=5, y=4) at hanshu.c:2
2 int max ( int x, int y){ return x>y?x:y;}
(gdb) print max
$3 = {int (int, int)} 0x80483c4
(gdb) print &max
$4 = (int (*)(int, int)) 0x80483c4
(gdb) print *max
$5 = {int (int, int)} 0x80483c4
(gdb) s
max (x=2, y=6) at hanshu.c:2
2 int max ( int x, int y){ return x>y?x:y;}
(gdb) s
main () at hanshu.c:8
8 f=min;
(gdb) print min
$6 = {int (int, int)} 0x80483d3
(gdb) print &min
$7 = (int (*)(int, int)) 0x80483d3
(gdb) print *min
$8 = {int (int, int)} 0x80483d3
(gdb) s
9 printf (" %d,%dt" , min (2,6), (f)(5,4));
(gdb) print f
$9 = (int (*)(int, int)) 0x80483d3
(gdb) print &f
$10 = (int (**)(int, int)) 0xbffff6ac
(gdb) print *f
$11 = {int (int, int)} 0x80483d3
(gdb) s
min (x=5, y=4) at hanshu.c:3
3 int min ( int x, int y){ return x
(gdb) s
min (x=2, y=6) at hanshu.c:3
3 int min ( int x, int y){ return x
(gdb) print min
$12 = {int (int, int)} 0x80483d3
(gdb) s
main () at hanshu.c:10
10 }
在調(diào)試的過(guò)程中我print了很多的信息,細(xì)心的讀者肯定能獲得更多的收獲,尤其是對(duì)變量f的print,讀者可以自己閱讀,學(xué)到更多的東西。我給出的只是一個(gè)參考的調(diào)試方式,希望讀者能夠舉一反三,自己對(duì)代碼進(jìn)行實(shí)際的調(diào)試,加深理解。
上面說(shuō)的都是用指針來(lái)實(shí)現(xiàn)函數(shù)的調(diào)用,接下來(lái)我們看一個(gè)用函數(shù)指針作為參數(shù)的用法。
#include
using namespace std;
typedef int (*print)(int );
int fun1(int i)
{
return (int)i;
}
void fun2(int j,print prt)
{
for(int k=0;k
cout<<'t'<
}
void main()
{
int i=10;
fun2(i,fun1);
}
運(yùn)行結(jié)果如下:
看了上面的描述,我想都對(duì)函數(shù)指針的概念有了大致的了解,另外一個(gè)希望大家不要混淆的概念就是指針函數(shù),,這兩個(gè)概念都是簡(jiǎn)稱,指針函數(shù)是指帶指針的函數(shù),即本質(zhì)是一個(gè)函數(shù)。我們知道函數(shù)都又有返回類型(如果不返回值,則為無(wú)值型,即為void),只不過(guò)指針函數(shù)返回類型是某一類型的指針。
其定義格式如下所示:
返回類型標(biāo)識(shí)符 *返回名稱(形式參數(shù)表)
{ 函數(shù)體}
返回類型可以是任何基本類型和復(fù)合類型。返回指針的函數(shù)的用途十分廣泛。事實(shí)上,每一個(gè)函數(shù),即使它不帶有返回某種類型的指針,它本身都有一個(gè)入口地址,該地址相當(dāng)于一個(gè)指針。比如函數(shù)返回一個(gè)整型值,實(shí)際上也相當(dāng)于返回一個(gè)指針變量的值,不過(guò)這時(shí)的變量是函數(shù)本身而已,而整個(gè)函數(shù)相當(dāng)于一個(gè)“變量”,關(guān)于函數(shù)的返回值問(wèn)題我將在下一章來(lái)講解,本章到此為止。希望以上內(nèi)容對(duì)你有所幫助!
C語(yǔ)言博大精深,由于本人水平有限,博客中的不妥或錯(cuò)誤之處在所難免,殷切希望讀者批評(píng)指正。同時(shí)也歡迎讀者共同探討相關(guān)的內(nèi)容,如果樂(lè)意交流的話請(qǐng)留下你寶貴的意見(jiàn)。
c語(yǔ)言相關(guān)文章:c語(yǔ)言教程
c++相關(guān)文章:c++教程
評(píng)論