Unable to Understand the declaration

extern void (*set_new_handler (void (*))) ();


I hope some of you guys have used above function. However I am not able to understand the way it is dec.lared.

1. Argument of set_new_handler is a pointer to a function, yes.. agreed. But it does seem to have a name. So how does the compiler invoke it when new is not able to allocate memory

2. I am not able to understand the declaration itself fully
Argument of set_new_handler is a pointer to a function, yes.. agreed. But it does seem to have a name. So how does the compiler invoke
I don't follow your reasoning.

I am not able to understand the declaration itself fully
It's a function that takes a function pointer and returns a function pointer. The pointer must point to a function that takes and returns nothing.
ok.. lets say we have the following declaration

int (*pt2Function)(float, char, char) = NULL;

Only because we have pt2Function as name of the function pointer, we are able to assign some function to it in the later part of the program as follows


pt2Function = &someFunction;

where the declaration of someFunction is as follows

int someFunction(float i,int j, int k);

but void (*)() doesnt have name at all..

It's a function that takes a function pointer and returns a function pointer. The pointer must point to a function that takes and returns nothing.



ok.. but I can only see that set_new_handler is function pointer which takes void (*)() as argument and returns void. What I am not able see is how/where is a funtion pointer returned?


I am visualising it as something like below

1
2
3
4
5
6
7
8
9
10
11
void function1(void(*X)())
{
//code;
}

void function2()
{
//code;
}

set_new_handler = &function1;


and to call set_new_handler, I am visualising the following syntax

set_new_handler(&function2);


I am sure I am wrong somewhere terribly.. I am not able to get it though!


Last edited on
but void (*)() doesnt have name at all.
That's right. Because it's a declaration, not a definition.
The int in the following example doesn't have a name, either:
int f(int);

What I am not able see is how/where is a funtion pointer returned?
The parts in bold are the return type, the underlined part is the symbol's name:
extern void (*set_new_handler (void (*)()))();
@ Helios

I know u are right but I am not able to understand.. Thats why I am posting this reply

extern void (*set_new_handler (XXX))();

where XXX is void (*)()

Now in , both (*set_new_handler (XXX))(); and void(*)() declarations are same. But how are u able to say that the former returns a function pointer while the latter returns a void.


Last edited on
For convenience, from now on a function that takes and returns nothing shall be called a "routine".

both (*set_new_handler (XXX))(); and void(*)() declarations are same.
Uh... No.
(*set_new_handler (XXX))(); is not a declaration. It's nothing; it's not syntactically valid. void (*set_new_handler (XXX))(); does return a pointer.
Now, this last valid declaration of set_new_handler is not the same as void(*)(). A void(*)() is a pointer to a routine.

--------------------------------------------------------------------------------

Look, this is what's going on:
A function pointer is declared as return_type (*optional_identifier)(parameter_list). A pointer to a routine is: typedef void (*routine_pointer)();

set_new_handler's declaration is equivalent to this: routine_pointer set_new_handler(routine_pointer);

The typedef above can be replaced with a macro: #define ROUTINE_POINTER(id) void (*id)()

Then the declaration can be written as ROUTINE_POINTER(set_new_handler(ROUTINE_POINTER()))

--------------------------------------------------------------------------------

Try to expand that last line of code and I think you'll understand what's going on.
Last edited on
Ok helios.. let me put a simple question..

void (*funcPtr) (char)

now the above is a pointer to a function that takes char and returns void rite?

Can you please modify the above declaration to return a function pointer?
void(*(*funcPtr)(char))();

(Usually you just add more typedefs instead of dealing with this nonsense.)
Last edited on
Topic archived. No new replies allowed.