2015-02-18 22 views
5

Gần đây tôi đã được đọc một mã, và phát hiện ra rằng một con trỏ hàm được viết như sau:Viết một con trỏ hàm trong c

int (*fn_pointer (this_args))(this_args) 

tôi thường gặp phải một con trỏ hàm như thế này:

return_type (*fn_pointer) (arguments); 

tương tự điều được thảo luận here:

// this is a function called functionFactory which receives parameter n 
// and returns a pointer to another function which receives two ints 
// and it returns another int 
int (*functionFactory(int n))(int, int) { 
printf("Got parameter %d", n); 
int (*functionPtr)(int,int) = &addInt; 
return functionPtr; 
} 

Ai đó có thể cho tôi biết sự khác biệt và cách thức công việc này?

+0

bạn có thể vui lòng đăng mã chính xác không? một mẫu compilable? –

+1

Điều này có thể giúp http: // stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work? rq = 1 –

+0

@ iharob: Mã của tôi dài hơn. Tôi vừa cập nhật. –

Trả lời

10
int (*fn_pointer (this_args))(this_args); 

tuyên bố fn_pointer như một hàm mang theo this_args và trả về một con trỏ tới một hàm mang theo this_args như là đối số và trả về một loại int. Nó tương đương với

typedef int (*func_ptr)(this_args); 
func_ptr fn_pointer(this_args); 

Hãy hiểu nó hơn một chút:

int f1(arg1, arg2); // f1 is a function that takes two arguments of type 
        // arg1 and arg2 and returns an int. 

int *f2(arg1, arg2); // f2 is a function that takes two arguments of type 
         // arg1 and arg2 and returns a pointer to int. 

int (*fp)(arg1, arg2); // fp is a pointer to a function that takes two arguments of type 
         // arg1 and arg2 and returns a pointer to int. 

int f3(arg3, int (*fp)(arg1, arg2)); // f3 is a function that takes two arguments of 
             // type arg3 and a pointer to a function that 
             // takes two arguments of type arg1 and arg2 and 
             // returns an int. 

int (*f4(arg3))(arg1, arg2); // f4 is a function that takes an arguments of type 
          // arg3 and returns a pointer to a function that takes two 
          // arguments of type arg1 and arg2 and returns an int 

How to readint (*f4(arg3))(arg1, arg2);

  f4       -- f4 
     f3( )      -- is a function 
     f3(arg3)      -- taking an arg3 argument 
     *f3(arg3)      -- returning a pointer 
    (*f3(arg3))( )     -- to a function 
    (*f3(arg3))(arg1, arg2)   --  taking arg1 and arg2 parameter 
    int (*f3(arg3))(arg1, arg2)   --  and returning an int 

Vì vậy, cuối cùng là một công việc nhà :). Hãy cố gắng tìm ra tuyên bố

void (*signal(int sig, void (*func)(int)))(int); 

và sử dụng typedef để xác định lại.

+0

Đối số nào là đối số và giá trị nào là đối số? –

+0

@SHIVENDRAMISHRA; Bây giờ đã rõ chưa? – haccks

4

Từ cdecl (mà là một công cụ hữu ích giúp đỡ để giải mã tờ khai C):

int (*fn_pointer (this_args1))(this_args2) 

khai báo fn_pointer như chức năng (this_args1) trở về con trỏ đến function (this_args2) trở int

Do đó, trước đây là hàm, trả về con trỏ cho hàm, trong khi sau:

return_type (*fn_pointer) (arguments);

là con trỏ "hoạt động bình thường".


Đọc thêm về cách kê khai lại các khai báo phức tạp từ bài viết Clockwise/Spiral Rule.

+2

'fn_pointer' không phải là một con trỏ trong khai báo này (chỉ là một hàm). – Wintermute

+0

@Wintermute: Tốt, cảm ơn. –

2

này

int (*fn_pointer (this_args1))(this_args2) 

tuyên bố một hàm mang theo như các đối số this_args1 và trả về một con trỏ hàm kiểu

int (*fn_pointer)(this_args2) 

do đó, nó chỉ là một hàm trả về một con trỏ hàm.

Các vấn đề liên quan