I am not new to C programming. But I don't understand what is usefulness to keep pointer to function as a structure member in C. e.g.
// Fist Way: To keep pointer to function in struct
struct newtype{
int a;
char c;
int (*f)(struct newtype*);
} var;
int fun(struct newtype* v){
return v->a;
}
// Second way: Simple
struct newtype2{
int a;
char c;
} var2;
int fun2(struct newtype2* v){
return v->a;
}
int main(){
// Fist: Require two steps
var.f=fun;
var.f(&var);
//Second : simple to call
fun2(&var2);
}
Does programmers use it to give Object Oriented(OO) shape to there C code and provide abstract object? Or to make code look technical.
I think, in above code second way is more gentle and pretty simple too. In fist way, we still have to pass &var
, even fun()
is member of struct.
If its good to keep function pointer within struct definition, kindly help to explain the the reason.
Providing a pointer to function on a structure can enable you to dynamically choose which function to perform on a structure.
This gives you the ability to have a single calling interface, but calling two different functions depending on if your test is valid or not.
Its useful if you're trying to do some sort of "object based" programming.
If you've ever seen the Quake 3 engine's source code.. you can see clearly that most "entities" have attributes that define them, and the work they do[which are the function pointers].
Segregating attributes and functions(through function pointers in C) defines a "struct" object's attributes and actions they can do.
For example:
Here is a project to help explain the usefulness of function pointers. Try to create a c-based library providing inheritance and polymorphism. Or, try and recreate "C with Classes". Function pointers inside structures will be vital. http://www.cs.rit.edu/~ats/books/ooc.pdf
Sometimes in C you need to call a function, without knowing its implementation upfront. E.g. if you're developing a stand-alone library used in different projects. In that case, it makes perfect sense to add a function pointer to a context struct and pass that along to the library functions. The library functions can then call your function at run-time without having to include the header files that define it.
It is indeed similar to what you do when doing Object Oriented programming. In C, you usually pass around context variables, e.g. a struct, grouping together a set of variables and possibly functions that are meaningful for that context. This is close to the eqvuivalent in OO programming, where you instantiate a class and set the required variables on the instance.
I've used this in the past to implement generic containers1 in C.
For example:
The list structure itself is data-agnostic, using
void *
to represent data items, and delegates all type-aware operations to the functions indicated by the pointers above:1. For suitably loose definitions of "generic" and "container"
This can be particuarly useful in embedded system, or driver writing. The functions are called using function pointers.
e.g.
etc