Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Practical use of pointers to functions!!!!!! 1

Status
Not open for further replies.

dakuneko

Programmer
Nov 17, 2000
19
SG
I have been studying C for quite a while now. I have reached the point where
I do not know when will I use pointers to functions. What is pointers to functions practical use?

Can someone give me a practical example? Or maybe
point me to a site where there are detailed discussion of the practical
use of pointers to functions.

I believe that this is a strong feature in
C so I really would like to learn it and understand it.

I would really appreciate all your help.


Zagato
 
Well, I've never used them before, but I could see how they'd be useful, because you could pass a pointer to a function to another function as an argument. I can't think of an example right now, but it would come in handy when you're sorting, etc.

also has some good info on what they're good for. As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Hi,
Another possible use could be that structutes in C
contain only data members, not functions. so you can
make a pointer to function a member of a structure.
I havent tried it, but if u do, please let me know what were the results and how useful it was.
 
Hello,

While interacting with hardware through C,e.g. writing TSRs,we often want to use ROM-BIOS functions which do not have names. So pointers are to be used for calling them.
I know only this use of pointers to functions.
 
One easy way to see the utility of pointers to functions is to look at the standard library function qsort() which sorts an array of objects. The final argument to qsort() is a pointer to a user-defined function. qsort() uses this function to compare items in the array. This feature makes qsort() very powerful because it allows you to sort an array that contains like objects of any data type -- the pointer to function you supply allows qsort() to be free from knowing anything about the representation of the objects.

Consider the following simple example:

#include <stdlib.h>
#include <string.h>

struct person {
char firstName[20];
char lastName[30];
};

/* This is called by qsort as needed */
int cmpNames(const void *p1,const void *p2)
{
struct person *person1=(struct person *)p1;
struct person *person2=(struct person *)p2;
int tmp;

tmp=strcmp(person1->lastName,person2->lastName);
if (0==tmp) {
/* They have the same last name, sort by
* first name
*/
return strcmp(person1->firstName,person2->firstName);
} else {
/* Return comparison of last names */
return tmp;
}
}

int main(void)
{
struct person people[50];

/* Populate list with 50 people */

qsort(&people,sizeof people/sizeof(struct person),sizeof(struct person),cmpNames);

return 0;
}

Another use of pointers to functions is in generic data structure libraries (such as linked lists and binary trees). In my own libraries, I often have routines that perform operations on all the items in a data structure where the operation is determined by a function supplied by the user. This frees the data structure from having to know anything about the objects it stores.

If you're unclear about any of the above or need more examples, post again.

HTH,

Russ
bobbitts@hotmail.com
 
I have stored function pointers in structure fields and pass this structure to other functions like MayankMangla said and call this function from my module. It was ptr to user defined functions for action in my &quot;File manager module&quot; for create, open .... some files.
Works fine:)
 
That's great Lim!

Could send me a sample code of this? I would really appreciate it! I guess MayankMangla would appreciate it too!

You could post it here or you could e-mail me. My address is jayit@yahoo.com.

Thanks! ~~~
&quot;In C++, only friends can touch your private parts.&quot; - Emmanuel Ackouoy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top