Pages

Tuesday, June 3, 2025

Function Pointer (the TOPIC that I missed!) - Part 1

 #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

Stack using Linked List – Partial (University Exam Topic)

 #include <iostream> using namespace std; struct Node {     int data;     Node* next; }; class Stack {     Node* top; public:     Stac...