2010-06-14 3 views
3

Можно создать дубликат:
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 
+0

Кто-то отредактировал и разместил в форме кода – geshafer

+1

Дубликат [Как происходит разыменование указателя функции?] (Http://stackoverflow.com/questions/2795575/how-does-dereferencing-of-a-function- pointer-happen) –

ответ

5

Я упрощу ваш вопрос, чтобы показать, что я думаю, что вы хотите знать.

Учитывая

typedef void (*mycall)(int a, int b); 
mycall f = somefunc; 

вы хотите знать, почему

(*f)(5, 2); 

и

f(5.2); 

сделать то же самое. Ответ заключается в том, что имя функции представляет собой «обозначение функции». От стандарта:

"A function designator is an expression that has function type. Except when it is the 
operand of the sizeof operator or the unary & operator, a function designator with 
type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to 
function returning type’’." 

При использовании оператора разыменования * на указатель на функцию, что разыменования также «функция целеуказатель». От стандарта:

"The unary * operator denotes indirection. If the operand points to a function, the result is 
a function designator;..." 

Так f(5,2) становится существенно (*f)(5,2) первым правилом. Это становится call to function designated by f with parms (5,2) вторым. В результате f(5,2) и (*f)(5,2) делают то же самое.

+2

думаю, что у вас есть небольшая опечатка, * mycall ---> myfunc – reuscam

+0

@reuscam: fixed, thanx. –

+0

Хороший ответ и отличная цитата. – sjsam

3

Поскольку указатели на функции автоматически решаются Используете ли вы их с или без оператора разыменования.

2

вы не должны использовать & перед именем функции

x[0] = addme; 
x[1] = subme; 
x[2] = mulme; 

однако оба пути справедливы.