Spread the love

Hacking: The Art of Exploitation has a really nice crash course on C. The book has a paragraph or two about function pointers.

Every time I look at function pointers, I find myself referring back to some documentation or getting confused (one too many *’s! or the parenthesis are wrong)

Here is a very simple example to demonstrate void function pointers, hopefully so I can remember in the future!.

#include <stdio.h>

void hello() {
	printf("Hello");
}

void world() {
	printf("World");
}

void space() {
	printf(" ");
}

void newline() {
	printf("n");
}

void print(const void *f) {
	void (*f_ptr)() = f;
	f_ptr();
}

int main(int argc, char **argv) {
	print(hello);
	print(space);
	print(world);
	print(newline);
	return 0;
}