2013-08-07 2 views
2

함수 포인터에보고 인수를 전달 :기능 포인터를 가지고, 내가 함수 포인터를 사용하는 코드에 문제가

#include <stdio.h> 
#include <stdlib.h> 

typedef void (*VFUNCV)(void); 

void fun1(int a, double b) { printf("%d %f fun1\n", a, b); } 
void fun2(int a, double b) { printf("%d %f fun2\n", a, b); } 

void call(int which, VFUNCV* fun, int a, double b) 
{ 
    fun[which](a, b); 
} 

int main() 
{ 
    VFUNCV fun[2] = {fun1, fun2}; 
    call(0, fun, 3, 4.5); 
    return 0; 
} 

을 그리고 오류 발생 :

/home/ivy/Desktop/CTests//funargs.c||In function ‘call’:| 
/home/ivy/Desktop/CTests//funargs.c|11|error: too many arguments to function ‘*(fun + (unsigned int)((unsigned int)which * 4u))’| 
/home/ivy/Desktop/CTests//funargs.c||In function ‘main’:| 
/home/ivy/Desktop/CTests//funargs.c|16|warning: initialization from incompatible pointer type [enabled by default]| 
/home/ivy/Desktop/CTests//funargs.c|16|warning: (near initialization for ‘fun[0]’) [enabled by default]| 
/home/ivy/Desktop/CTests//funargs.c|16|warning: initialization from incompatible pointer type [enabled by default]| 
/home/ivy/Desktop/CTests//funargs.c|16|warning: (near initialization for ‘fun[1]’) [enabled by default]| 
||=== Build finished: 1 errors, 4 warnings ===| 

내가 사용하는 Code :: Blocks 그것을 컴파일하십시오. fun1

typedef void (*VFUNCV)(int , double); 

#include <stdio.h> 
#include <stdlib.h> 

typedef void (*VFUNCV)(void); 

void fun1() { printf("fun1\n"); } 
void fun2() { printf("fun2\n"); } 

void call(int which, VFUNCV* fun) 
{ 
    fun[which](); 
} 

int main() 
{ 
    VFUNCV fun[2] = {fun1, fun2}; 
    call(1, fun); 
    return 0; 
} 

답변

8

함수 포인터가 함수 선언에 맞지 않습니다. 식료품.

typedef void (*VFUNCV)(int, double); 
으로 정의 해보십시오.
6

수정 typedeffun2 유형 intdouble 두 개의 인수를 받아들이는 : 내가 인수를 가지고 있지만 일부, 나는 혼란있어 그나마

그것의 간단,

관련 문제