the first of my question is about pointers to functions
i dont even know why do we need them
cant we just use functions as one of the arguments to another function
if we need to use pointers to functions to use as them arguments how do we declare these pointer and what is the right syntax to pass function pointers to functions
do we have to use void as pointer's function arguments like
are function names, pointers to their functions?
i would be very grateful if you could tell me how to define and use pointers to functions
my second question is
what is this
if lineptr is declared like this
a character pointer array
is this a redeclaration of the type this array?
can we rearrange the types like this
There are many different reasons to use function pointers. Some of those are:
- CallBack functions where you pass a function to another function, and the called function doesn't know how to do something (e.g a generic sort function given a compare function for the data to be sorted - that way you can sort up, down, alphabetical order, length order or some other order by just changing the compare function, not the sort function). Another typical callback operation is validation of data-input - say you have a function to read a string from the user, and you want to use it to confirm that the user has input something that is a valid-looking e-mail address, phone number or name - so pass along a function that can determine if the string looks like (e-mail, phone number or name) - e.g. a persons name shouldn't contain numbers or special characters, e-mail address should contain @, phone number should start with X and then contain Y number of digits with optional spaces and dashes, possibly + or parenthesis thrown in for good measure).
- Table-based programming, e.g. you build a table of menu-choices (strings, key-presses, selections in a (X-)Windows menu or such), along with the action for each menuitem as a function pointer.
- Instance-based differences (sort of like member functions in object orientation): Say you have a generic function to draw graphical objects, and you pass along a function that knows how to draw the specific instance (circle, square, oval, rectangle, etc).
- Hardware/software architecture differences - a function pointer passed points to either the SSE or "regular math" version of a function to do the same thing, or one version of Windows has a clever way to do something, whilst in the older version, the same thing is either not availabe (so you do a simpler thing) - have a function pointer that points to the "advanced" or "simple" version.
These are just some of the categories of function pointer use.
This looks like a type-cast, where some function is expecting a pointer to a pointer of void, but the actual type coming out of the previous declaration makes it pointer to pointer of char (the address of lineptr, and lineptr is a pointer to char, which makes the whole thing a pointer to pointer of char).
thanks matsp but why void **
does it change the type of the lineptr pointer's adress which is a char
and if that is so why not
(void *)(* lineptr)
and cant we just use functions as arguments
and are function names, pointers to that functions
In the case of the cast, all it does is change the type (basicly saying "Yes, I know you, the compiler, thinks it's a char **, but I say now that it's a void **") - it doesn't change the value, just what type it is.
You can do "void *p = (void *) 1000;" - this casts the integer 1000 to a void pointer - so p would have the value 1000.
Function pointers are variables that hold the address of a function. A function name is an identifier of a function - it "is" the address of the function, just written with words - it would be hard to remember which is which if you have functions "called" 1234, 1243 a 1324 for example.
So if we have a function called "abc", that has the address of 1234, and a function pointer called "fp", this is what the code would look like:
/// ... do something useful here ..
fp = abc; // This sets fp to the value 1234 (address of abc).
fp(); // calls abc.
(*fp)(); // alternative stype for fp();
abc(); // call abc directly.
// All of the above three lines do exactly the same thing.
thank you very much matsp
but ididnt understand this
(*fp)(); // alternative stype for fp();
doesnt (*fp) already call for abc function because it is a pointer