可能重複:
How does dereferencing of a function pointer happen?函數指針的使用
大家好, 爲什麼這兩個碼給出相同的輸出, 案例1:
#include <stdio.h>
typedef void (*mycall) (int a ,int b);
void addme(int a,int b);
void mulme(int a,int b);
void subme(int a,int b);
main()
{
mycall x[10];
x[0] = &addme;
x[1] = &subme;
x[2] = &mulme;
(x[0])(5,2);
(x[1])(5,2);
(x[2])(5,2);
}
void addme(int a, int b) {
printf("the value is %d\n",(a+b));
}
void mulme(int a, int b) {
printf("the value is %d\n",(a*b));
}
void subme(int a, int b) {
printf("the value is %d\n",(a-b));
}
輸出:
the value is 7
the value is 3
the value is 10
案例2:
#include <stdio.h>
typedef void (*mycall) (int a ,int b);
void addme(int a,int b);
void mulme(int a,int b);
void subme(int a,int b);
main()
{
mycall x[10];
x[0] = &addme;
x[1] = &subme;
x[2] = &mulme;
(*x[0])(5,2);
(*x[1])(5,2);
(*x[2])(5,2);
}
void addme(int a, int b) {
printf("the value is %d\n",(a+b));
}
void mulme(int a, int b) {
printf("the value is %d\n",(a*b));
}
void subme(int a, int b) {
printf("the value is %d\n",(a-b));
}
輸出:
the value is 7
the value is 3
the value is 10
有人編輯並放入代碼形式請 – geshafer 2010-06-14 15:17:58
重複[如何解除函數指針的引用?](http://stackoverflow.com/questions/2795575/how-does-dereferencing-of-a-function-指針 - 發生) – 2010-06-14 15:21:34