When to use complex declarations like the following.....?

A

Alan Holloway

Hi all,

Firstly thanks for your golden insight on my earlier post re bitwise
operations.

I now have another question!

I've just been reading the excellent Peter van der Linden excerpt [1]
from sun.com (i'm ordering the book when I get paid), and I am getting
to grips with the rules for understanding the complex declarations,
but, and this may sound like I am missing the bigger picture; when
would you use such a declaration like the following?

char * const *(*next)();

I think I am exposing a knowledge gap in regards to function pointers,
as i'm also not so clear when and why one would need to use one, but I
am hoping to hammer it in.. <OT> I understand that signal() needs a
function pointer to return to callback on receiving an arbitrary
signal,</OT> but i'm not sure of any other scenarios of when using a
function pointer would be warranted, and I guess that's the kind of
guidance i'm looking for.

I've seen www.function-pointer.org, but I still cannot figure out
*why* you would need one.

My apologies if this post seems somewhat disorganised or unclear, but
I knew what I meant at the time :)

Many thanks for your help

Al.

References:
[1] http://www.sun.com/971124/cover-linden/cchap.html
 
R

Ravi Uday

Alan Holloway said:
Hi all,

Firstly thanks for your golden insight on my earlier post re bitwise
operations.

I now have another question!

I've just been reading the excellent Peter van der Linden excerpt [1]
from sun.com (i'm ordering the book when I get paid), and I am getting
to grips with the rules for understanding the complex declarations,
but, and this may sound like I am missing the bigger picture; when
would you use such a declaration like the following?

char * const *(*next)();

Declares 'next' as a pointer to a function returning pointer to a const
returning pointer to a char
and takes no/void parameters.

So essentially if you have a real function of the above prototype then you
can just
assign the address of that function to 'next' fn pointer and invoke it
like any ordinary function does.
next = my_function;
next(); /* This will actually execute 'my_function' */

This approach increases the maintainability and appearance of your code.
I think I am exposing a knowledge gap in regards to function pointers,
as i'm also not so clear when and why one would need to use one, but I
am hoping to hammer it in.. <OT> I understand that signal() needs a
function pointer to return to callback on receiving an arbitrary
signal,</OT> but i'm not sure of any other scenarios of when using a
function pointer would be warranted, and I guess that's the kind of
guidance i'm looking for.

I've seen www.function-pointer.org, but I still cannot figure out
*why* you would need one.

My apologies if this post seems somewhat disorganised or unclear, but
I knew what I meant at the time :)

Many thanks for your help

Al.

References:
[1] http://www.sun.com/971124/cover-linden/cchap.html
 
J

Jens.Toerring

Alan Holloway said:
I've just been reading the excellent Peter van der Linden excerpt [1]
from sun.com (i'm ordering the book when I get paid), and I am getting
to grips with the rules for understanding the complex declarations,
but, and this may sound like I am missing the bigger picture; when
would you use such a declaration like the following?
char * const *(*next)();

No idea when you would really need something exactly like that...
But since, according to that page, "next is a pointer to a function
returning a pointer to a read-only pointer-to-char" that could be
a function that lets you iterates over a list of (read-only) strings,
perhaps wrapping around back to the start of the list when you reach
the end, or giving you a randomly selected string each time you call
the function.
I think I am exposing a knowledge gap in regards to function pointers,
as i'm also not so clear when and why one would need to use one, but I
am hoping to hammer it in.. <OT> I understand that signal() needs a
function pointer to return to callback on receiving an arbitrary
signal,</OT> but i'm not sure of any other scenarios of when using a
function pointer would be warranted, and I guess that's the kind of
guidance i'm looking for.

Obviously, function pointers are always needed when you have callback
fucntions that you have to pass somehow to the function that installs
the callback. But probably the canonical example of a place where you
need a function pointer is the standard C function qsort(). Since it
is supposed to operate on arrays of arbitrary objects it needs to know
which function actually does the comparisons for the elements of the
array of objects you pass it.

But there are lots of other cases. Let's assume you're going to
write a calculator program that takes text input from the user
and you want to allow the use of a set of built-in functions.
Then a simple way to realize that would be to have an array
of structures like this

struct funcs {
char *func_name;
void ( * func_ptr ) ( void );
} func_arr[ ] = { { "func1", func1 },
{ "func2", func2 },
{ "func3", func3 } };

and when the user now types in some function name (stored in the char
array 'user_input') you can iterate over that array, trying to find
the function name and calling the associated function on success:

for ( i = 0; i < sizeof func_arr / sizeof *func_arr; i++ )
if ( ! strcmp( user_input, func_arr[ i ].func_name ) ) {
func_arr[ i ].func_ptr( );
break;
}

if ( i >= sizeof func_arr / sizeof *func_arr ) {
fprintf( stderr, "Sorry, no such function: %s\n", user_input );
exit( EXIT_FAILURE );
}

Other examples might include cases where you want to invoke different
sets of functions depending on the state of the program while avoiding
to check the current state over and over again before each function
call. In that case you could use have a set of function pointers that
get assigned the addresses of the appropriate functions whenever the
state of the program changes. For example, you might have a program
that is running all of the time but should behave differently during
the day and during the night. Then you could simply replace what the
pointers (by which you call the relevant functions) are pointing to
whenever you switch from day to night mode (or the other way round).

Finally, but here we're getting out of the realm of standard C, is
the use of functions that get loaded by some plug-in mechanism
while the program is already running. In that cases you typically
have function pointers in the main program that get assigned "useful"
values when you load that plug-in (e.g. a shared library), so that
you now can call them. You can't hardcode that functions into your
program because you don't know what these functions are going to be
until you load the plug-in (and the linker can't create a program
if it doesn't know about all the referenced functions in your program
but will quite happly accept it when you're working with function
pointers since these aren't functions but just well-defined variables,
even though they don't point to anything "useful" while you link).

Regards, Jens
 
D

Darrell Grainger

Hi all,

Firstly thanks for your golden insight on my earlier post re bitwise
operations.

I now have another question!

I've just been reading the excellent Peter van der Linden excerpt [1]
from sun.com (i'm ordering the book when I get paid), and I am getting
to grips with the rules for understanding the complex declarations,
but, and this may sound like I am missing the bigger picture; when
would you use such a declaration like the following?

char * const *(*next)();

I think I am exposing a knowledge gap in regards to function pointers,
as i'm also not so clear when and why one would need to use one, but I
am hoping to hammer it in.. <OT> I understand that signal() needs a
function pointer to return to callback on receiving an arbitrary
signal,</OT> but i'm not sure of any other scenarios of when using a
function pointer would be warranted, and I guess that's the kind of
guidance i'm looking for.

Have a look at the bsearch() or qsort() function. They require a function
pointer. Additionally, I might want to create a generic function myself.
Then my function will require a function pointer.

I might also want to change the function being used based on some run-time
event. For example, I have twenty functions but I only ever need to use
one a day. Today the system signals me to use function 17. I initialize
the function pointer to point at function 17 and let the rest of the
program run. At midnight my program gets a signal indicating function 5 is
the function for the day. I initialize the pointer to function 5 and let
the program continue running.
I've seen www.function-pointer.org, but I still cannot figure out
*why* you would need one.

My apologies if this post seems somewhat disorganised or unclear, but
I knew what I meant at the time :)

Many thanks for your help

Al.

References:
[1] http://www.sun.com/971124/cover-linden/cchap.html
 
P

pete

Darrell said:
Have a look at the bsearch() or qsort() function.
They require a function
pointer. Additionally, I might want to create
a generic function myself.
Then my function will require a function pointer.

I might also want to change the function being used
based on some run-time event. For example,
I have twenty functions but I only ever need to use
one a day. Today the system signals me to use function
17. I initialize
the function pointer to point at function 17 and
let the rest of the
program run. At midnight my program gets a signal
indicating function 5 is the function for the day.
I initialize the pointer to function 5 and let
the program continue running.

An array of function pointers, is a handy way to
select message handlers for different kinds of messages.
 
A

Alan Holloway

pete said:
An array of function pointers, is a handy way to
select message handlers for different kinds of messages.


hi all, thanks for your great responses!

I'm beginning to understand I think. I basically need to see
functions in this context as just another variable. The am/pm analogy
is a good one - function pointers serve as selectors for any arbitrary
function in a list of functions.

Ok! I'll have to have a play with them in some code then methinks,
that's the way to hammer it in after all!

Once again thanks for your time and insight!

Al
 
A

Alan Holloway

pete said:
An array of function pointers, is a handy way to
select message handlers for different kinds of messages.

Just a quick question, but would function pointers have anything to do
with program scope? I.e, would you ever pass a non-pointed to function
to the likes of qsort()? I mean, would said function need to be
pointed-to to be "found" ??

thanks

Alan
 
J

Jens.Toerring

Alan Holloway said:
Just a quick question, but would function pointers have anything to do
with program scope? I.e, would you ever pass a non-pointed to function
to the likes of qsort()? I mean, would said function need to be
pointed-to to be "found" ??

What do you mean by "pass a non-pointed to function"?

You can have a situation where the function you pass to qsort() is
in a different source file. During compilation you only need a
declaration of the function in scope, but not the definition. It's
then the job of the linker to put in the correct address. I.e.
if you have something like

#include <stdio.h>
#include <stdlib.h>

int sort_ints( const void *a, const void *b )

int main( void )
{
int a[ ] = { 42, 128, -7 };
qsort( a, sizeif a / sizeof *a, sizeof *a, sort_ints );
return EXIT_SUCCESS;
}

the compiler will be satisfied, even if the function sort_ints()
is defined in a different source file - only the linker will have
to make sure that function can be found.

And if you pass an explicit function pointer (i.e. a variable of
type pointer to the required type of function) to qsort() neither
the compiler nor the linker will complain because they simply can
not check which function that pointer is pointing to - this decision
may be made e.g. only while the program is already running (it may
use set the function pointer to different functions depending on if
it's AM or PM;-) Of course, that opens up the possibility that the
function pointer isn't pointing to someting useful - then the pro-
gram will probably crash (unless you are real unlucky).

Regards, Jens
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top