R
ramu
Can anyone tell me the advantages of using function pointers?
Thanks in advance.
Thanks in advance.
Can anyone tell me the advantages of using function pointers?
Richard said:ramu said:
They're very handy when you need to point to functions.
Take a look at qsort and bsearch, in the standard library. How do
you think they manage to process any kind of array without knowing
what kind of array it is or which way up you like your data? (And
bear in mind that it might be an array of structs, and qsort has
no idea what criteria you use to determine which way round two
structs should be ordered.)
Advantages over what?ramu said:Can anyone tell me the advantages of using function pointers?
ramu said:Can anyone tell me the advantages of using function pointers?
Thanks in advance.
ramu said:Ok. Let me put it in this way. At what situations can we use function
pointers?
ramu said:Ok. Let me put it in this way. At what situations can we use function
pointers?
ramu said:Can anyone tell me the advantages of using function pointers?
A function pointer is also needed to register a function with theS.Tobias said:Think of functions (algorithms) as your data. Answer yourself why you
use variables in your programs. For the same reason you use function
pointers - they are variables that hold your functions as data (well,
actually they only hold references - but that's only how far you can get
with the C language). In some languages, but not in C, (some) functions
(or generally code) are objects, and you can pass them around and do
operations on them as operands.
Can anyone tell me the advantages of using function pointers?
A function pointer is also needed to register a function with the
atexit() function. A function name used without parentheses
or parameter list...*is* a function pointer. So in some sense, any
time you invoke a function, you are "using" a function pointer.
That's right; even a function identifier *with* parentheses is firstCharles Richmond said:A function pointer is also needed to register a function with the
atexit() function. A function name used without parentheses
or parameter list...*is* a function pointer. So in some sense, any
time you invoke a function, you are "using" a function pointer.
But there's an ambiguity in the spoken
language where "pointer" means "a value" (your and Standard's usage) and
"a variable of pointer type" (which is what I, and presumably OP, meant).
Emmanuel Delahaye said:S.Tobias a écrit :
This 'value' is clearly an address.
- A pointer is an object. It has an address and a value.
- The value of a pointer is an address or NULL.
- A pointer constant has a value but no address. (Constant expression)
- The value of a pointer constant is an address or NULL.
Keith said:Sure, using the unqualified word "pointer" only to refer to pointer
objects and using "address" to refer to pointer values, make sense.
Unfortunately, it's not what the standard does. For example, the
description of malloc() says:
The malloc function returns either a null pointer or a pointer to
the allocated space.
So I'm afraid we're stuck with the ambiguity.
Joe Wright said:Keith Thompson wrote: [snip]Sure, using the unqualified word "pointer" only to refer to pointer
objects and using "address" to refer to pointer values, make sense.
Unfortunately, it's not what the standard does. For example, the
description of malloc() says:
The malloc function returns either a null pointer or a pointer to
the allocated space.
So I'm afraid we're stuck with the ambiguity.
But you and I can still use the terms correctly if we will.
If a non-C programmer asks "what is a pointer?" the correct answer is "aKeith Thompson said:Sure, if there were any agreement on what "correctly" means.
Malcolm said:If a non-C programmer asks "what is a pointer?" the correct answer is "a
variable which holds an address".
However it is unidiomatic to use the term "address" in a C context.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.