function pointers

R

Richard Heathfield

ramu said:
Can anyone tell me the advantages of using function pointers?

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.)
 
C

CBFalconer

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.)

They allow chunks of code to be easily customized to an
application. In the case of my hashlib when the table is created
it is passed five functions, of four different types, that totally
customize the hash table to the application.

You can see the code in question at:

<http://cbfalconer.home.att.net/download/hashlib.zip>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
I

Ian Collins

ramu said:
Can anyone tell me the advantages of using function pointers?
Advantages over what?

Where do you want to use them and what to you intend to gain?
 
R

ramu

Ok. Let me put it in this way. At what situations can we use function
pointers?
Regards
 
R

Rod Pemberton

ramu said:
Can anyone tell me the advantages of using function pointers?

Thanks in advance.

Yes, there are a number of typical uses. A common one would be for the
action to be taken when a menu item is selected. Your code has a array of
generic menu structures each of which has an action associated with the menu
item. You can store a function pointer in the array and then use the same
code to call the appropriate function for that menu item. Another one is
when you have an address for a function outside your application space and
need to call the function. This is useful for calling the BIOS reset in RM
DOS. The ability to do this is environment specific. Another one, similar
to the menu action, would be a table of addresses of ISRs, perhaps to be
used to setup a bunch of GDT/IDT selectors. Typically, internal operating
system filesystem structures use pointers to the various file routines.
This allows a single structure to support different filesystems using
different functions with the same structure (similar to menu's).

//Boot OW1.3 code
void (__far *reset)();
reset=MK_FP(0xFFFF,0x0000);
(*reset)(); /* call the function */

//ISR's DJGPP snippet
typedef void(*isr_func)(void);
isr_func isrs[]={
isr00,isr01,isr02, /*etc... */, isrff
};
// use isrs[x] as address for GDT/IDT setup

//menu items DJGPP snippet
typedef struct {
int bogus1;
int bogus2;
int (* action)();
} menu_item;
typedef struct {
int name;
int x,y; // coordinates
menu_item item[32];
} menu;
menu my_menus[]={
{ 0,0, function1} // 'int *function1(void) must exist somewhere...
{ 2,2, function2} // ditto
}
(*my_menus[0].menu_item[6].action)(void); // sixth menu item on menu 0,
which is 'int *function1(void)'


Rod Pemberton
 
C

Chris Dollin

ramu said:
Ok. Let me put it in this way. At what situations can we use function
pointers?

Whenever a function pointer is expected?

When the code needs to perform some action, but that action is not
(perhaps cannot, perhaps should not be) known in advance.
 
I

Ian Collins

ramu said:
Ok. Let me put it in this way. At what situations can we use function
pointers?

One example that springs to mind is a protocol stack or a driver API
where each module is required to provide a number of entry points
defined in a struct.

For example,

given an interface defined as:

struct DiverInterface
{
void (*initFn)(void);
int (*readFn)( void*, unsigned );
int (*writeFn)( const void*, unsigned );
};

Somewhere in your code:

extern void myInit(void);
extern int myRead( void*, unsigned );
extern int myWrite( const void*, unsigned );

void init(void)
{
struct DiverInterface interface;

interface.initFn = myInit;
interface.readFn = myRead;
interface.writeFn = myWrite;

/* pass to API */
}
 
S

S.Tobias

ramu said:
Can anyone tell me the advantages of using function pointers?

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.
 
C

Charles Richmond

S.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.
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.
 
E

Emmanuel Delahaye

Charles Richmond a écrit :
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.

ITYM a 'function pointer constant', just like the name of an array is a
'pointer constant'. A 'pointer' is definitely a variable:

/* p is a pointer */
char *p = "hello";
int *p = malloc(sizeof *p * 2);
void f(char *p);

/* x is a[n object] pointer constant */
#define x NULL
char x[] = "hello";
int x[123];

x();
 
S

S.Tobias

Charles 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.
That's right; even a function identifier *with* parentheses is first
converted to pointer to function. 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).
What I tried to explain to OP is that the reasons for using variables
of type pointer to function become more obvious once you start to treat
functions (only conceptually) as objects.
 
E

Emmanuel Delahaye

S.Tobias a écrit :
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).

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.
 
K

Keith Thompson

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.

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.
 
J

Joe Wright

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.

But you and I can still use the terms correctly if we will.
 
K

Keith Thompson

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.

Sure, if there were any agreement on what "correctly" means.
 
M

Malcolm

Keith Thompson said:
Sure, if there were any agreement on what "correctly" means.
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.
 
K

Keith Thompson

Malcolm said:
If a non-C programmer asks "what is a pointer?" the correct answer is "a
variable which holds an address".

Is it? I suppose it depends on which language the programmer is
familiar with.
However it is unidiomatic to use the term "address" in a C context.

C uses the term "address" all over the place; see the unary "&"
operator, for example.

If you consistently use "pointer" to refer to pointer objects, and
"address" to refer to values, your terminology will be entirely
consistent with the C standard -- but the language used in the C
standard won't be entirely consistent with yours.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top