#include <iostream>
using namespace std;
// Function declaration + definition (function to be pointed to..)
void greet() {
cout << "Hello!\n";
}
int main() {
// Function pointer
void (*fp)();
// Assign the address of greet() fnx to the fnx pointer..
fp = greet;
greet();
return 0;
}
OR
with parameters
#include <iostream>
using namespace std;
int add(int a, int b) {
return a+b;
}
int main() {
// Declare function pointer
int (*operation)(int, int);
// Assign fnx address
operation = add;
// Call the fnx using pointer
cout << operation(4, 4);
return 0;
}
No comments:
Post a Comment