473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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
Nov 14 '05 #1
7 1369

"Alan Holloway" <al***@kiaro.net> wrote in message
news:fa**************************@posting.google.c om...
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

Nov 14 '05 #2
Alan Holloway <al***@kiaro.net> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
On Wed, 4 Aug 2004, Alan Holloway wrote:
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


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #4
Darrell Grainger wrote:

On Wed, 4 Aug 2004, Alan Holloway wrote:

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.


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

--
pete
Nov 14 '05 #5
pete <pf*****@mindspring.com> wrote in message news:<41***********@mindspring.com>...
Darrell Grainger wrote:

On Wed, 4 Aug 2004, Alan Holloway wrote:

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.


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
Nov 14 '05 #6
pete <pf*****@mindspring.com> wrote in message news:<41***********@mindspring.com>...
Darrell Grainger wrote:

On Wed, 4 Aug 2004, Alan Holloway wrote:

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.


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
Nov 14 '05 #7
Alan Holloway <al***@kiaro.net> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Carl-Olof Almbladh | last post by:
Dear all, In C99, complex is now built in, and the C library contains functions for complex exp, etc. It is not so easy to find info about the syntax if one is not willing to buy the ISO/ANSI...
0
by: masood.iqbal | last post by:
I have always had trouble correctly interpreting complex type declarations without using the "declarator parser" program given in K&R. Then someone taught me about the "Clockwise" Rule. Start with...
12
by: noor.fatma | last post by:
Hi, recently I attended a test on C where I was asked to declare the following declarations in one line. void ** ( *p )(int *, char *); int *a; how should i do this shall i use typedef...
14
by: David Marsh | last post by:
Can someone show me or point me to an example of declaring and initializing complex numbers in C99? Also, in looking at complex.h, I don't see any library calls for + - * or /. How are the basic...
11
by: nospam | last post by:
Is there a utility that takes an arbitrarily complex C language declaration, checks it validity, and breaks it down into something more understandable. Is there a executable version for wintel...
2
by: Osiris | last post by:
Is there a program that can explain complex C declarations like char (*(*x)( )) like so: x is array of pointers to functiion returning pointer to array of char ???
9
by: void main | last post by:
I'm rather new to complex numbers in C and was wondering, how do I initialize a complex variable properly if the imaginary part is 0. I tried -------- #include <complex.h> float complex c...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.