473,624 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1385

"Alan Holloway" <al***@kiaro.ne t> wrote in message
news:fa******** *************** ***@posting.goo gle.com...
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.ne t> 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***********@p hysik.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*****@mindsp ring.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*****@mindsp ring.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.ne t> 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***********@p hysik.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
1819
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 standard document, despite the fact that implementations are already available Also, because it is built in, one cannot figure out the syntax from the header files. I have a few questions
0
257
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 the declared item's name and "spiral outwards" in a clockwise direction. With this rule I was able to guess the type correctly about half the time (which was still a great improvement). I recently came across an old (June 1987) article in the...
12
2080
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 for it.
14
2413
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 arithmetic operations carried out? David Marsh
11
1888
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 platforms?
2
1857
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
9939
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 = 1.0f; --------
0
8233
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8170
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8619
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.