Function Pointers

D

Daniel Rudy

Hello Group,

Please consider the following code:

static const mod_cmdft_t cmd_func_tab[] = {
{MODSEC_CMD_SETPARAM, sizeof(mod_param_t), &mod_setparam},
{MODSEC_CMD_GETPARAM, sizeof(mod_param_t), &mod_getparam}
};

These types are defined as follows:

/* aux param: get/set parameter */
typedef struct mod_param_tag__ mod_param_t;
struct mod_param_tag__
{
int param; /* parameter id */
int value; /* parameter value */
};

/* internal: function call table (do not use) */
typedef struct mod_cmdft_tag__ mod_cmdft_t;
struct mod_cmdft_tag__
{
int command; /* command id */
unsigned long psize; /* parameter block size */
int (*fp)(void *pargs); /* pointer to handler function */
};


What is the proper way of setting the address in the function pointer?
To use & or not to use &?


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
B

Ben Pfaff

Daniel Rudy said:
What is the proper way of setting the address in the function pointer?
To use & or not to use &?

& is completely optional when taking the address of a function.
There's no technical reason to choose to use it or not to use it.
As a matter of style, I'd suggest being consistent, whichever way
you choose to go.

I would probably omit the &.
-- [...]
Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep

"fcsk"?
 
S

santosh

Daniel said:
Hello Group,

What is the proper way of setting the address in the function pointer?
To use & or not to use &?

I don't think there's a difference between the two methods. I
personally use & for the sake of uniformity.
 
D

Daniel Rudy

At about the time of 7/8/2007 7:30 PM, Ben Pfaff stated the following:
& is completely optional when taking the address of a function.
There's no technical reason to choose to use it or not to use it.
As a matter of style, I'd suggest being consistent, whichever way
you choose to go.

I would probably omit the &.

Ok, and what construct should I use to call it? Right now, I'm using this:

error = (*cmd_func_tab[cidx].fp)(ptr);

error and cidx is an int. ptr is a void *. Gcc does not give any error
or warnings when doing this.

This is my first time out using function pointers, which is why I'm asking.
-- [...]
Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep

"fcsk"?

You know, all this time, I never noticed that, and you are the first one
to point that out. It should be fsck instead.


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fsck more fsck yes spray umount sleep
 
B

Ben Pfaff

Daniel Rudy said:
Ok, and what construct should I use to call it? Right now, I'm using this:

error = (*cmd_func_tab[cidx].fp)(ptr);

error and cidx is an int. ptr is a void *. Gcc does not give any error
or warnings when doing this.

Again, it's just a matter of style. fp(arg), (*fp)(arg), and
(*****fp)(arg) are all equally valid if fp is a function pointer,
or for that matter the name of a function.

I tend to write fp(arg).

Some argue that (*fp)(arg) is better style than fp(arg) because
the latter sometimes makes programmers look for a function named
fp instead of for an object named fp.

I haven't heard anyone argue for (*****fp)(arg).
 
R

Richard Heathfield

Ben Pfaff said:

Again, it's just a matter of style. fp(arg), (*fp)(arg), and
(*****fp)(arg) are all equally valid if fp is a function pointer,
or for that matter the name of a function.

I tend to write fp(arg).

Some argue that (*fp)(arg) is better style than fp(arg) because
the latter sometimes makes programmers look for a function named
fp instead of for an object named fp.

I won't try to claim that (*fp)(arg) is /better/ style, but it's the
style I use and prefer, for the reason you state.
I haven't heard anyone argue for (*****fp)(arg).

Give it time.
 
D

Daniel Rudy

At about the time of 7/8/2007 10:09 PM, Ben Pfaff stated the following:
Five dereferences is a sign of insanity.

Who could keep it straight?

The only time that I have seen THAT many dereferences is in Unix kernel
source code, and even then, it's part of some complicated struct like
proc or something.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fsck more fsck yes spray umount sleep
 
R

Richard

Ben Pfaff said:
Daniel Rudy said:
Ok, and what construct should I use to call it? Right now, I'm using this:

error = (*cmd_func_tab[cidx].fp)(ptr);

error and cidx is an int. ptr is a void *. Gcc does not give any error
or warnings when doing this.

Again, it's just a matter of style. fp(arg), (*fp)(arg), and
(*****fp)(arg) are all equally valid if fp is a function pointer,
or for that matter the name of a function.

I tend to write fp(arg).

Some argue that (*fp)(arg) is better style than fp(arg) because

The style I prefer since it does stand out and makes it obvious that you
are using function pointers.
the latter sometimes makes programmers look for a function named
fp instead of for an object named fp.

I haven't heard anyone argue for (*****fp)(arg).

--
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top