Where can we use function pointers in C, what uses for function pointers -
I have recently been asked in an interview: Where can we use the function pointer in C? And what the function pointer returns I said that we can call the function using function pointer, then he asked some examples, but I could not satisfy some other examples with it. He then asked me what function pointer returns I told them that this function depends on the pointers announcement.
But I would really like to know some of the function pointer in C.
I think there is a classical example in C. Quote from there (and I know that this is, so there is a very good site, but this is correct). . (* Comparator) (Constant Worth *, Constant Wide *));
function that compares the two elements function will follow this prototype:
int comparator (constant empty zero * Elem1, const void * elem2);
The function should accept two parameters that are indications of elements, are typed as zero * These parameters should be returned to some data types and They should be compared.
The return value of this function should be shown to see whether elem1 is considered to be equivalent to returning, equal to, or more by returning, respectively, a negative value, zero, or a positive value.
There is another "classical" example calculator (see, for example it is C ++ but it is similar in C).
You have four math functions, for example
float plus (float a, float b) {a + b return; } Float minus (float a, float b) {return a-b; } Float times (float A, float b) {return a * b; } Float Divide (Float A, Float B) {Return A / B; }
In some way you should select your operation
/ * Here should be one or any switch / case that chooses the correct operation * / float (* PtrFunc) (float, float) = plus;
and you can call it later (for some reason you do not want to call it directly in
if / switch , possibly because you do "Things" that are "normal" for all tasks, such as logging or printing results)
float result = ptrFunc (1.0f, 2.0f);
Callback (as written by
vine'th ) and "poor man" virtual functions (you can use Function Points for Two other things in them
struct and when you create
struct (you know, like using the "poor man" constructor, but if it is, Say), you can save there from which functions will be used by that
struct .) < / Html>
Comments
Post a Comment