C Function Pointers

Programming, for all ages and all languages.
Post Reply
Puffy
Member
Member
Posts: 26
Joined: Mon May 26, 2008 7:00 am

C Function Pointers

Post by Puffy »

Hey,

I'm planning on abstracting away from hardware using some function pointers. The way I was planning to implement this was something like:

Code: Select all


void (*pt2Cls) (uint8_t colour) = 0;

void init_console( void (*cls) (uint8_t colour))
{
	pt2Cls = cls;
}

So my init_console() function prototype will be pretty long (once I include all of the functions). Is there a better, or more concise way of doing this?

Cheers,

Pete
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: C Function Pointers

Post by Alboin »

Linux uses structs of function pointers, and assigns functions using the GCC '.fieldname=' initialization method.

For an example, the ARM Linux branch uses it a lot for the various devices and such.
C8H10N4O2 | #446691 | Trust the nodes.
Puffy
Member
Member
Posts: 26
Joined: Mon May 26, 2008 7:00 am

Re: C Function Pointers

Post by Puffy »

Great, cheers guys.
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: C Function Pointers

Post by CodeCat »

Essentially this is similar to the way virtual functions work in C++. Each C++ class that has virtual functions contains a pointer to a compiler-provided data structure (the VTable), which has a list of pointers to the virtual functions of that class. Conceptually it's similar to this (pseudo) C/C++ code:

Code: Select all

int YourClass_func1(int);
void YourClass_func2(char*, float);

struct YourClass_VTable
{
    int (*func1)(int) = &YourClass_func1;
    void (*func2)(char*, float) = &YourClass_func2;
} YourClass_VTable;

struct YourClass
{
    YourClass_VTable* vtable_ptr = &YourClass_VTable;
    // other variables...
};

// Inside another function
YourClass foo;

// Calling a virtual function
foo->vtable_ptr->func1(42);
In your situation, you can optimise somewhat by removing the VTable and providing the pointers to the functions in the struct (class) directly, but the principle is the same.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: C Function Pointers

Post by Creature »

Or you could do something that looks a lot like the structure way; you could do it like Glut and other libraries do; before calling the init_console function (the function that would have a lot of parameters), you could create several functions like this:

Code: Select all

void SetClsCallback(void (*CallbackFunc) (uint8_t colour))
{
   pt2Cls = CallbackFunc;
}
Though if you have a lot of these functions, you'd get loads of these functions (mutators in a certain way). And to read them you'd have to make accessors. You could just make the variables public or use the, as said before, structure way (which is more elegant in my opinion).
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: C Function Pointers

Post by DeletedAccount »

Just use typedef #-o , if all you want to do is make it shorter and meaning full , it can be used within structures as well

Code: Select all

#include <stdio.h>
#include <stdlib.h>

typedef void (* FPTR)();
void exper()
{
  puts("MU ha  ha ha ha ");
}
int main()
{
   FPTR r = exper;
   puts("Bwi ha ha ha ha ");
   (*r)();
   return EXIT_SUCCESS; 

}
Regards
Shrek
Post Reply