Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Pointers To Functions

linked list

  • Please log in to reply
No replies to this topic

#1 Soryy708


    CC Resident

  • Banned
  • PipPipPipPip
  • 59 posts

Posted 13 April 2012 - 11:42 PM

Before you read on, please note that this tutorial covers a pretty advanced topic.
To properly understand it, you need:
  • Proper understanding of Functions
  • Proper understanding of Pointers
  • Basic understanding of Structs (optional)
Still here? Awesome!

As a C programmer, you probably know that pointers play a major role in your life.
If you've ever used an other programming language, you probably encountered classes, which probably could hold functions in them.
As you might have noticed, C does not let you have a function inside a struct... Well, at-least not in a conventional way.

Functions, just like variables, are too stored in memory. Thus, they can be reffered to by adress, so a variable to store and adress that adress can be used. Thus, a pointer to a function is possible.
Here's a fun fact: C has absolutely no problems with pointers inside Structs.
You know already that in C, you work with pointers by using the '*' thingy.
How-ever, it can get tricky; like in the case of a pointer to a function.

Take a look at that:
int* Func()
What is this? That's a function returning a pointer to an int.
int *Func()
That too is a function returning a pointer to an int.
How would you then "fool" the language into making a pointer to a function?
Posted Image
int (*Func)()

If you haven't been exposed to the influence of other languages, you might ask:
"How could that be useful?"
The answer is quite simple. Abstraction (design), convenience, and proceduralism.
Ever wondered how atexit() worked? Ever wondered what's atexit anyway?
atexit is a function which takes, as a parameter, a pointer to a function; which is called when the exit(0) or abort() functions are called.

Here's another example:
Say you want to make a framework.
You define the program flow, you write utility functions, etc.. But there's no point to all of that.
The programmer using your framework can not get what he wants without modifying your framework, or at-least the main function.
However, by providing him a way to push a function he had written himself into a queue which you then execute, you solve that issue and make his life easy.

Yet another example:
Even in older games, you've got different kinds of objects. Heck, even in pong!
Every object has it's own logics behind it. What does it do upon collision? What does it do every frame?
Sure, you could fall for it and write explicitly all of the logics for all the objects, but there's yet another way: You can just call the, say, automation function of each object.
It makes the code look a-lot simpler, feel way more natural, and be way more clear; and obviously, flexible.

Here's an actual example:
#include <stdio.h>
#include <stdlib.h>

struct Obj
	void (*Func)();
	struct Obj* Next;
} Queue;

int PushFunc(void (*Function))
	static struct Obj* Cur = &Queue;
	static char IsFirst = 1;
	if(IsFirst == 1)
		Cur->Func = Function;
		IsFirst = 0;
		Cur->Next = malloc(sizeof(struct Obj));
		if(Cur->Next == NULL) /* malloc failed */
			return 0;
		Cur = Cur->Next;
		Cur->Func = Function;
		Cur->Next = NULL;

	return 1;

void DoLogics()
	puts("Doing the logics... Like a sir!");
	struct Obj* Cur = &Queue;
	while(Cur != NULL)
		Cur = Cur->Next;

void SayHi()
	puts("Hello there!");

int main()
		return 1;

	return 0; /* Note: There's no freeing of the linked list */
Sure, the above code is useless; but use your imagination!
You can probably see how this in-valuable part of the language be useful.

  • 1

Also tagged with one or more of these keywords: linked list

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download