Velocity Reviews > Newbie Ques: ((void(*) (void *) 0)

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

Desmond Foley
Guest
Posts: n/a

 06-07-2005
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

Zoran Cutura
Guest
Posts: n/a

 06-07-2005
Desmond Foley <(E-Mail Removed)> wrote:
> 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

--
Z ((E-Mail Removed))
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond

Lawrence Kirby
Guest
Posts: n/a

 06-07-2005
On Tue, 07 Jun 2005 13:09:48 +0100, Desmond Foley wrote:

> 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

James Daughtry
Guest
Posts: n/a

 06-07-2005
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.

c_learner
Guest
Posts: n/a

 06-07-2005

Desmond Foley wrote:
> 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

usually, that's kind of form is used for hardware addressing.

Desmond Foley
Guest
Posts: n/a

 06-07-2005
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

Richard Bos
Guest
Posts: n/a

 06-07-2005
Desmond Foley <(E-Mail Removed)> wrote:

> 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