![]() |
|
#1
|
|||
|
|||
Use of function pointersHi all,
I found in a tutorial on function pointers that apart from being used for Callback functions, the function pointers can also be used to replace switch statments. Please find below the example code for the same : CPP / C++ / C Code:
I dont find any use of function pointers or even the need for switch statements here. A direct call to the function plus could be made in the function Replace_A_Switch instead of calling Switch. Hence, i could not appreciate the use of function pointers in this context. Can anyone explain me better the use of function pointers other than for call back functions ? Hoping to get a positive resoponse. Thanks & Regards, KK2202 Last edited by LuciWiz : 09-May-2008 at 08:08.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
|||
|
|||
Re: Use of function pointersConsider the following:
CPP / C++ / C Code:
Code:
I realize that you may still consider this 'callbacks' but it would be pretty hard to write a program that did anything useful without having this functionality. The fact that you can invoke code at another location by essentially aliasing that block with a name instead of copying all into main (which itself has the int (*)() properties; ponder that) is pretty stinkin' nice. |
|
#3
|
|||
|
|||
Re: Use of function pointersQuote:
I personally feel that use of things like pointers to functions should be discussed after there has been enough exposition that the student can get a flavor of their significance by seeing a real, practical example. For example, the standard C library funciton qsort is quite flexible with the things that It can sort. In order to sort stuff, you have to tell it something about the stuff (places in memory where the items are located and how much memory the items take up). You also have to feed qsort the address of a function that you are going to use to compare data items. That is, you have a pointer to the function. For simple things like arrays, the comparison function just returns a plus or minus value, depending on whether the thing pointed to by the first argument is greater or less than the second item. Depending on the logic inside the comparison function, the array can be sorted in ascending order or descending order. CPP / C++ / C Code:
Output Code:
So, except for the rather exotic-looking function prototype declarations, it still doesn't look much like an example of the use of pointers to functions. (The fourth argument to qsort is a pointer to function, but we just used the function name rather than a separate variable whose type is pointer to function.) So, let's go a little further. A more interesting example might be to have an array of two pointers to functions. One would have a value that the address of compar1, and the other would have a value that is the address of compar2. Then the program could prompt the user to select whether he/she wanted the sort to be in ascending order or descending order, and the proper pointer would be used to call qsort. CPP / C++ / C Code:
Code:
You can do Stuff Like That with function ponters. Regards, Dave |
|
#4
|
|||
|
|||
Re: Use of function pointersThank you very much Dave and l7sqr for taking out time and explaining the stuff with examples.
|
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| run script command on ns2.26 | newbie06 | Computer Software Forum - Linux | 65 | 19-Aug-2009 08:50 |
| [Tutorial] Function Pointers | aaroncohn | C++ Forum | 4 | 17-Feb-2006 12:33 |
| [Tutorial] Pointers in C (Part II) | Stack Overflow | C Programming Language | 0 | 27-Apr-2005 18:36 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 19:35 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The