Question about signal()/pointers to functions that return pointers to functions
Author |
Message |
WANG CHARLT #1 / 12
|
 Question about signal()/pointers to functions that return pointers to functions
While looking through stuff on signals, I became a little confused at the functionality of the signal() function. From what I understand, signal() takes an int (the sig) and a pointer to a function (the handler) as the parameters. However, the function signal() is defined as the following: void (*signal(int sig, void (*handler)(int)))(int) and is supposed to return a function pointer as the return value. My problem lies in the fact that, looking at the declaration, I would have THOUGHT that signal, itself, was a pointer to a function but I guess I'm wrong (I didn't find anything about this in the FAQ---or maybe I didn't look closely enough). ANYWAY, so to clear up my confusion, how would I, say declare a variable to be a function pointer that returns a function pointer. Would it be something like this: void (*(void (*myFunctionPointer)(void))(void) Anyway, thanks for your help and I apologize if this question is a straight- forward one that can be found in the FAQ. Thanks, Charlton -- Copyright (C) TODAY by ME. Use of | - Good Pings Come in Small Packets. this e-mail without my permission | - You would never get any free pizza if God is STRICTLY prohibited. | owned Pizza Pizza (he always delivers).
|
Sat, 30 Aug 1997 14:44:42 GMT |
|
 |
WANG CHARLT #2 / 12
|
 Question about signal()/pointers to functions that return pointers to functions
Quote: >While looking through stuff on signals, I became a little confused at the >functionality of the signal() function. From what I understand, signal() >takes an int (the sig) and a pointer to a function (the handler) as the >parameters. However, the function signal() is defined as the following: > void (*signal(int sig, void (*handler)(int)))(int) >and is supposed to return a function pointer as the return value. My problem >lies in the fact that, looking at the declaration, I would have THOUGHT that >signal, itself, was a pointer to a function but I guess I'm wrong (I didn't >find anything about this in the FAQ---or maybe I didn't look closely enough). >ANYWAY, so to clear up my confusion, how would I, say declare a variable to >be a function pointer that returns a function pointer. Would it be something
Sorry...I, for some strange reason, seem to have overlooked the extra pair of brackets after the signal so the functionality of signal is straightforward. However, if someone could still answer my second question, that would be great. Quote: >like this: > void (*(void (*myFunctionPointer)(void))(void)
I know this one doesn't work :( Thanks, Charlton -- Copyright (C) TODAY by ME. Use of | - Good Pings Come in Small Packets. this e-mail without my permission | - You would never get any free pizza if God is STRICTLY prohibited. | owned Pizza Pizza (he always delivers).
|
Sat, 30 Aug 1997 15:02:57 GMT |
|
 |
WANG CHARLT #3 / 12
|
 Question about signal()/pointers to functions that return pointers to functions
Quote: >Sorry...I, for some strange reason, seem to have overlooked the extra pair >of brackets after the signal so the functionality of signal is straightforward. >However, if someone could still answer my second question, that would be great. >>like this: >> void (*(void (*myFunctionPointer)(void))(void) >I know this one doesn't work :(
Well, don't I feel like the biggest idiot in the world...how the heck did that extra void get there? DOH...Anyway, I tested this declaration without the second void and it works so I guess it is acceptable. If not, please let me know (before I make an ass out of myself again). Thanks, Charlton -- Copyright (C) TODAY by ME. Use of | - Good Pings Come in Small Packets. this e-mail without my permission | - You would never get any free pizza if God is STRICTLY prohibited. | owned Pizza Pizza (he always delivers).
|
Sat, 30 Aug 1997 15:17:26 GMT |
|
 |
Jos Horsmei #4 / 12
|
 Question about signal()/pointers to functions that return pointers to functions
|While looking through stuff on signals, I became a little confused at the |functionality of the signal() function. From what I understand, signal() |takes an int (the sig) and a pointer to a function (the handler) as the |parameters. However, the function signal() is defined as the following: | | void (*signal(int sig, void (*handler)(int)))(int) | |and is supposed to return a function pointer as the return value. My problem |lies in the fact that, looking at the declaration, I would have THOUGHT that |signal, itself, was a pointer to a function but I guess I'm wrong (I didn't |find anything about this in the FAQ---or maybe I didn't look closely enough). |ANYWAY, so to clear up my confusion, how would I, say declare a variable to |be a function pointer that returns a function pointer. Would it be something |like this: | | void (*(void (*myFunctionPointer)(void))(void) | |Anyway, thanks for your help and I apologize if this question is a straight- |forward one that can be found in the FAQ. The way of declaring things in C is awkward, especially if you have to deal with pointers to arrays/functions etc. `typedef' is your friend here: typedef void (*pfv_t)(int); here `pfv_t' is declared to be of type: pointer to a function (taking one int parameter) returning a void. The `signal' function can now be declared using this typedef as: pfv_t signal(int sig, pfv_t handler); which looks a lot neater than the original, doesn't it? BTW, the original declaration you gave above is correct too; it reads: void (*signal(int sig, void (*handler)(int)))(int) \-5-/ 3\--1-/\--------------2---------------/\-4-/ signal (1) is a function ((2) is the paramter list), returning a pointer (3) to a function ((4) is its paramter list) which returns a void (5). The paramter list itself (2) reads as follows: (int sig, void (*handler)(int)) \--1--/ \-5-/ 3\--2--/ \-4-/ the first parameter (1) is named `sig' and it's an int. The second parameter `handler' (2) is pointer (3) to a function which takes one int parameter (4) and returns a void (5). I think I prefer the typdef'd version ... kind regards,
|
Sat, 30 Aug 1997 23:54:38 GMT |
|
 |
Lawrence Kubic #5 / 12
|
 Question about signal()/pointers to functions that return pointers to functions
Quote: > ANYWAY, so to clear up my confusion, how would I, say declare a variable to > be a function pointer that returns a function pointer. Would it be something > like this: > void (*(void (*myFunctionPointer)(void))(void)
How about: void *(*func)(void)(void) I read this as "func is a pointer to a function with a (void) argument list that returns a pointer to a function with a (void) argument list that returns void". Larry K
|
Sun, 07 Sep 1997 07:13:55 GMT |
|
 |
Tanmoy Bhattachar #6 / 12
|
 Question about signal()/pointers to functions that return pointers to functions
<snip> |> How about: |> |> void *(*func)(void)(void) |> |> I read this as "func is a pointer to a function with a (void) argument |> list that returns a pointer to a function with a (void) argument list |> that returns void". Read again! void Z; Z is void (invalid) 1 void *Z; Z is pointer to 1 2 void *Z(void); Z is function returning 2 3 void *Z(void)(void); Z is function returning 3 (invalid) 4 void *(*Z)(void)(void); Z is pointer to 4 (invalid) 5 (1 is invalid only because the type of Z cannot be completed. 4 is invalid as a _type_ itself because a function cannot return a function: so 5 is also invalid). Now let us see how to build up what you described: Z is 1 void void Z; 2 function returning 1 void Z(void); 3 pointer to 2 void (*Z) (void); 4 function returning 3 void (*Z(void)) (void); 5 pointer to 4 void (*(*Z)(void)) (void); What is this type called? Just omit the Z above to get correctly void (*(*)(void))(void) Do you see the rules? They are: 1) Break up the declaration into base types (step 1) and one of `function returning', `array of', `pointer to', `struct containing' and `union of' types already declared. 2) Declare step 1 trivially (Use a variable name even if you are looking for the name of the type). This name is usually a keyword or a typedef name or a struct followed by a tag, or a union followed by a tag, or an enum optionally followed by a tag optionally followed by the enumerator list. 3) For `array of' or `function returning' append [] or () to the _variable name_ in the above. 4) For `pointer to' : if the variable name is immediately followed by [] or (): put a set of parantheses (i.e. ()) around the variable name and put a * inside this newly inserted group before the variable name. else put a * before the variable name. 5) For `struct containing' or `union of' put the whole declaration inside a struct {}. You may give the struct or union a tag if you need to refer to it again. (In recursive struct or union declarations, all but the `last' (in this breakup) instance of struct _must_ be thought of as an elementary tagged struct type of step 1). Now go back and for each [], fill in the dimension of the array if it is known. It must be known if [] is immediately followed by a []. (As a function cannot return an array [] cannot be immediately be followed by (), nor can a () be immediately followed by a ()). Now if you wanted a type, drop the variable name. Finally, go back to each () and fill it up with declaration of its parameters if known. A vararg function must have at least one fixed parameter and is declared by putting in a ,... after all the fixed parameters. Hope this helps Cheers Tanmoy --
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545 Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>, <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/ internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html> fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
|
Tue, 09 Sep 1997 00:51:02 GMT |
|
 |
Sam Hulic #7 / 12
|
 Question about signal()/pointers to functions that return pointers to functions
Quote:
>> ANYWAY, so to clear up my confusion, how would I, say declare a variable to >> be a function pointer that returns a function pointer. Would it be something >> like this: >> void (*(void (*myFunctionPointer)(void))(void) >How about: > void *(*func)(void)(void)
Try this. This is a complex example, however. We have a function "whee" which returns nothing and takes an int. So, our func pointer is of type void and takes an int. Now then, the 'neato' func is strange. We have this: void (*neato(char *data))(int) { .... definition } This means that the neato() func takes a string. It does NOT take an int! the (int) indicates that it is to return a func that returns void and takes an int. Make sense? Look over it a feew times, you'll get it. Try this code out: void whee(int); void (*neato(char *))(int); void whee(int num) { printf("YAY! passed number: %d\n", num); Quote: }
void (*neato(char *data))(int) { printf("now inside neato. string=%s\n", data); return whee; Quote: }
void main() { void (*fptr)(int); fptr = neato("success"); fptr(92); Quote: }
-- ___________________________________________________________________________ _
| Home page: http://copper.ucs.indiana.edu/~shulick/ -------------+--------------------------------------------------------------
|
Wed, 10 Sep 1997 16:20:14 GMT |
|
 |
B. Wiec #8 / 12
|
 Question about signal()/pointers to functions that return pointers to functions
SH>
SH>>> SH>>> ANYWAY, so to clear up my confusion, how would I, say declare a variable to SH>>> be a function pointer that returns a function pointer. Would it be something SH>>> like this: SH>>> SH>>> void (*(void (*myFunctionPointer)(void))(void) SH>> SH>>How about: SH>> SH>> void *(*func)(void)(void) SH> SH>Try this. This is a complex example, however. We have a function SH>"whee" which returns nothing and takes an int. So, our func pointer is SH>of type void and takes an int. Now then, the 'neato' func is strange. SH>We have this: SH> void (*neato(char *data))(int) SH> { .... definition } SH>This means that the neato() func takes a string. It does NOT take an SH>int! the (int) indicates that it is to return a func that returns void SH>and takes an int. Make sense? Look over it a feew times, you'll get it. SH>Try this code out: SH> SH>void whee(int); SH>void (*neato(char *))(int); SH> SH>void whee(int num) SH>{ SH> printf("YAY! passed number: %d\n", num); SH>} SH> SH>void (*neato(char *data))(int) SH>{ SH> printf("now inside neato. string=%s\n", data); SH> return whee; SH>} SH> SH>void main() SH>{ SH> void (*fptr)(int); SH> SH> fptr = neato("success"); SH> fptr(92); SH>} Personally I would prefer main() like the one below: main() { neato("success")(92); :-) Quote: }
Regards, B. Wiecek ------------------------------------------------------------------- /* All standard disclaimers included here */ #ifdef DISCLAIMER #include "blahblah.blah" #endif -------------------------------------------------------------------
|
Thu, 11 Sep 1997 02:21:19 GMT |
|
 |
Greg Bla #9 / 12
|
 Question about signal()/pointers to functions that return pointers to functions
Quote:
>How about: > void *(*func)(void)(void) >I read this as "func is a pointer to a function with a (void) argument >list that returns a pointer to a function with a (void) argument list >that returns void".
This is completely wrong. Please don't confuse other learners with misconceptions. If you find this hard, get cdecl and use it (input line broken for readability): cdecl> declare p as pointer to function (void) returning pointer to function (void) returning void void (*(*p)(void ))(void ) You can use it the other way as well, with its `explain' command. --
|
Fri, 12 Sep 1997 05:36:14 GMT |
|
 |
Dan P #10 / 12
|
 Question about signal()/pointers to functions that return pointers to functions
Quote:
>>How about: >> void *(*func)(void)(void) >>I read this as "func is a pointer to a function with a (void) argument >>list that returns a pointer to a function with a (void) argument list >>that returns void". >This is completely wrong. Please don't confuse other learners with >misconceptions. If you find this hard, get cdecl and use it (input >line broken for readability): > cdecl> declare p as pointer to function (void) returning pointer > to function (void) returning void > void (*(*p)(void ))(void ) >You can use it the other way as well, with its `explain' command.
The problem is that cdecl will not detect the original example as being illegal and will output some gibberish instead: cdecl> explain void *(*f)(void)(void) declare f as pointer to function (void) returning function (void) returning pointer to void Dan -- Dan Pop CERN, CN Division
Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
|
Fri, 12 Sep 1997 07:32:52 GMT |
|
 |
Jason Zubari #11 / 12
|
 Question about signal()/pointers to functions that return pointers to functions
What is the significance of returning function pointers, and, what exactly are they(function pointers, that is.) And no, this isn't a joke. :) -- Jason Zubarik -/- **CAUTION** C++ learner in progress..
Orlando, FL USA -/- a pit stop!" -- Grimmy The 'lil big elf.. -/-
|
Sat, 01 Nov 1997 03:00:00 GMT |
|
|
|