Skip to content

Instantly share code, notes, and snippets.

@RolandWarburton
Last active September 6, 2019 08:04
Show Gist options
  • Save RolandWarburton/f5685ca7558f1a99631c068387a48c77 to your computer and use it in GitHub Desktop.
Save RolandWarburton/f5685ca7558f1a99631c068387a48c77 to your computer and use it in GitHub Desktop.
c++ function pointers
#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