| |

VerySource

 Forgot password?
 Register
Search
View: 687|Reply: 6

How to take a function as a formal parameter into another function for execution?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-10-15 09:30:02
| Show all posts |Read mode
Like the title, please tell me more in detail. Thank you
Reply

Use magic Report

0

Threads

25

Posts

19.00

Credits

Newbie

Rank: 1

Credits
19.00

 China

Post time: 2020-10-15 10:45:01
| Show all posts
Speak with code

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

void fun1( int i, double d ){
  printf("fun1: i=%d d=%f\n",i,d);
}


void fun2( int i, double d ){
  printf("fun2: i=%d d=%f\n",i,d);
}

void F(int x, double y, void (*func)( int, double ))
{
        func(x,y);

}

int main()
{
  F(1,2,&fun1);
  F(1,2,&fun2);
  exit(0);

}
Reply

Use magic Report

0

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 United States

Post time: 2020-10-15 11:15:01
| Show all posts
Just pass a function pointer type to the formal parameters of the function.
The one upstairs is correct. . . .
Reply

Use magic Report

0

Threads

59

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-10-15 11:45:01
| Show all posts
With function pointer
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-10-15 12:00:01
| Show all posts
Function pointer
Reply

Use magic Report

1

Threads

27

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-10-15 12:15:01
| Show all posts
1231456
correct
Reply

Use magic Report

0

Threads

12

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-10-15 12:30:01
| Show all posts
It is recommended to use typedef to define function pointers
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list