Page 1 of 1

C Function Pointers

Posted: Sun Jan 04, 2009 2:26 pm
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

Re: C Function Pointers

Posted: Sun Jan 04, 2009 2:34 pm
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.

Re: C Function Pointers

Posted: Sun Jan 04, 2009 4:34 pm
by Puffy
Great, cheers guys.

Re: C Function Pointers

Posted: Sun Jan 04, 2009 4:42 pm
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.

Re: C Function Pointers

Posted: Mon Jan 05, 2009 9:37 am
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).

Re: C Function Pointers

Posted: Mon Jan 05, 2009 1:11 pm
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