Over a million developers have joined DZone.

Function Pointers

DZone's Guide to

Function Pointers

Free Resource
// Using function pointers to decide which function to run at runtime

int plus(int a, int b) { return a+b; }
int minus(int a, int b) { return a-b; }
int calc(int a, int b, int (*funcptr)(int, int)){ return funcptr(a,b); }
int main(int argc, char **argv) {
	int x = 11;
	int y = 7;
	int z = calc(x, y, +);
	printf("z: %i\n",z); // z: 18
	z = calc(x, y, −);
	printf("z: %i\n",z); // z: 4
	return 1;

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}