function pointer demo added
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:17 +0100
changeset 24 9cdad6c45b47
parent 23 7acfc5eda7ed
child 25 6cf883f9c506
function pointer demo added committer: Markus Bröker <mbroeker@largo.homelinux.org>
Makefile
function_pointers.c
--- a/Makefile
+++ b/Makefile
@@ -40,7 +40,8 @@
 	mem2swap \
 	prog_limit \
 	connection \
-	copy
+	copy \
+	function_pointers
 
 .SUFFIXES: .c .cc .asm
 
@@ -220,6 +221,10 @@
 	@echo Linking $< ...
 	@$(CPP) -o $@ $<
 
+function_pointers: function_pointers.o
+	@echo Linking $< ...
+	@$(CC) -o $@ $<
+
 .PHONY: clean uninstall
 
 clean:
new file mode 100644
--- /dev/null
+++ b/function_pointers.c
@@ -0,0 +1,40 @@
+/**
+ * test/demos/function_pointers.c
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef struct T {
+	int a;
+	int b;
+} T;
+
+int plus(T t)
+{
+	return t.a+t.b;
+}
+
+int minus(T t)
+{
+	return t.a-t.b;
+}
+
+int func(T t, int (*ptrFunc)(T))
+{
+	return ptrFunc(t);
+}
+
+int main(int argc, char **argv)
+{
+	T t = {
+		.a = 20,
+		.b = 10
+	};
+
+	printf("Result: %d\n", func(t, &plus));
+	printf("Result: %d\n", func(t, &minus));
+
+	return EXIT_SUCCESS;
+}