Newbie Ques: ((void(*) (void *) 0)

D

Desmond Foley

Hi

I have the following statement

#define STATIC ((void(*) (void *) 0)


The (void *) I understand as meaning a pointer to a void, so

(void *) 0

would be a void pointer to zero

the meaning of void(*) is stopping me.

I'd appreciate a translation.


Regards

Des
 
Z

Zoran Cutura

Desmond Foley said:
Hi

I have the following statement

#define STATIC ((void(*) (void *) 0)

I think you're missing a closing paran here. This would probably be

#define STATIC ( ( void (* ) (void *)) 0 )
The (void *) I understand as meaning a pointer to a void, so

(void *) 0

would be a void pointer to zero

the meaning of void(*) is stopping me.

I'd appreciate a translation.

actually in this case the (void *) is a functions parameter list
declaration.

The void (*) () part tells us that it should be a pointer to a function
returning nothing (void).

So we have 0 casted to a pointer to function expecting a void-pointer
and returning void.

HTH, HAND
 
L

Lawrence Kirby

Hi

I have the following statement

#define STATIC ((void(*) (void *) 0)

Note the parentheses don't match here, I'll assume you mean

#define STATIC ((void(*) (void *)) 0)

A cast contains a type which is similar to a declaration without the
variable name. Consider where the variable name would go in a
corresponding declaration:

void(*var) (void *);

Why is it there? There simply isn't anywhere else it could go. This
declares var as a pointer to a function taking a void * argument and
returning void. I've preserved the spacing which turns out to be a bit
confusing, this would be better written as:

void (*var)(void *);

so your original #define might be better as

#define STATIC ((void (*)(void *)) 0)
The (void *) I understand as meaning a pointer to a void, so

(void *) 0

would be a void pointer to zero

the meaning of void(*) is stopping me.

The spacing makes it look like that but it isn't. Remembering the
declaration of var, the cast is to a pointer to a function taking a void *
argument and returning void. So the expansion of STATIC evaluates to a
null pointer of this type.

Note that as a separate issue (void (*))p is a valid cast, however the
inner parentheses are redundant and it is equivalent to (void *)p.

Lawrence
 
J

James Daughtry

You've got mismatched parentheses in that macro, but typically if you
see (*) then it's part of a cast to a pointer to T where T would be a
function or array.
 
D

Desmond Foley

Hi

Thanks Zoran and Lawerence.


Yes I omitted a ')'


it should have been

#define STATIC ((void(*) (void *)) 0)



I now understand that


(void(*) (void *))


is a cast, and the cast is to a pointer to a function. That function has
one argument, a pointer to a void, the function returns void.


I think I can imagine how that applies if we were casting a function,
but I am unclear as to what it means for a number to be casted into a
function.


I am guessing now that the cast is to allow the zero (STATIC) to be
accepted as an argument to a function, that expects a pointer to a
function that has a single arg, a void pointer and returns a void.


Regards
 
R

Richard Bos

Desmond Foley said:
I now understand that

(void(*) (void *))

is a cast, and the cast is to a pointer to a function. That function has
one argument, a pointer to a void, the function returns void.

I think I can imagine how that applies if we were casting a function,
but I am unclear as to what it means for a number to be casted into a
function.

For any other number, it would mean nothing whatsoever.

However, the number in question was a literal 0. That's a null pointer
constant. Null pointer constants can be cast to function pointer type -
and the result is a null pointer of function pointer type. IOW, you get
a function pointer that explicitly points nowhere, just as (int *)0 is
an int pointer that points nowhere.

Richard
 

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