Last active
September 6, 2019 08:04
-
-
Save RolandWarburton/f5685ca7558f1a99631c068387a48c77 to your computer and use it in GitHub Desktop.
c++ function pointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <string> | |
#include <array> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
int functionToCall(int x) | |
{ | |
return x; | |
} | |
int bar(int fcnptr) | |
{ | |
cout << "fcnptr returned " + to_string(fcnptr) << endl; | |
return fcnptr; | |
} | |
// passing pointers | |
void printer(int *a) | |
{ | |
// an address of the pointer | |
cout << &a << endl; | |
// an address of what the pointer is pointing at | |
cout << *a << endl; | |
// the value that the pointer is pointing at | |
cout << *a << endl; | |
} | |
int main() | |
{ | |
using validateFcn = int(int); | |
validateFcn *fcnptr = functionToCall; | |
cout << bar(fcnptr(1)) << endl; | |
// passing pointers | |
int i = 1; | |
int *a; | |
a = &i; | |
printer(a); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment