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

Contains duplicate - using HashSet (optimized code)

  class Solution {     public boolean containsDuplicate ( int [] nums) {         HashSet< Integer > seen = new HashSet<>()...