Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: void * and pointers to function.

1,041 views
Skip to first unread message

Andrej Borsenkow

unread,
Jul 1, 1997, 3:00:00 AM7/1/97
to

I often have problems, compiling packages wich pass function as
parameters. Because these packages are known to compile on dozens of
system, I have a feeling, that in this case it is _my_ compiler who gets
it wrong. Here is the example:

void a(void);
void b(void *);

void (*x)(void);

void c(void *p)
{
b(a);
x = p;
p = x;
}

My compiler reports the error in all three lines of function c(). The
call to b(a) is invalid, because:

Argument type 'ptr to funct ( void ) ret void' does not match parameter
type 'ptr to void'

Next two lines just report incompatible pointer types.

Well, I thought, void * is always compatible with any other pointer type
(including pointer to function) - obviously, I am wrong.

Here is the question: does it comply with ANSI standard? If not, could
anybody provide reference to corresponding section?

much thanks in advance

-------------------------------------------------------------------------
Andrej Borsenkow Fax: +7 (095) 252 01 05
SNI ITS Moscow Tel: +7 (095) 252 13 88

NERV: borsenkow.msk E-Mail: borsen...@sni.de
-------------------------------------------------------------------------

Clive D.W. Feather

unread,
Jul 1, 1997, 3:00:00 AM7/1/97
to

In article <Pine.SV4.3.95.970701124016.8231D-100000@itsrm1>, Andrej
Borsenkow <b...@itsmx1.mow.sni.de> writes

>I often have problems, compiling packages wich pass function as
>parameters. Because these packages are known to compile on dozens of
>system, I have a feeling, that in this case it is _my_ compiler who gets
>it wrong.

No, it's you.

>void a(void);
>void b(void *);
>
>void (*x)(void);
>
>void c(void *p)
>{
> b(a);

The required argument type is (void *). The passed argument type is
(void (*)(void)). These are not assignable to each other. Function
pointers can *NOT* be stored in a void *.

> x = p;

Same problem in reverse: x has type (void (*)(void)) and p has type
(void *). Not assignable.

> p = x;

Original problem again.

Note that:

x = a;

is fine, because the types are the same, and

x = (void (*)(void)) b;

is fine with the case, because function pointers can always be cast to
each other's types.

>Well, I thought, void * is always compatible with any other pointer type
>(including pointer to function) - obviously, I am wrong.

No, it is *NOT* compatible with pointer to function. Only with pointer
to object or incomplete type.

>Here is the question: does it comply with ANSI standard? If not, could
>anybody provide reference to corresponding section?

6.2.2.3.

--
Clive D.W. Feather | Director of Software Development | Home email:
Tel: +44 181 371 1138 | Demon Internet Ltd. | <cl...@davros.org>
Fax: +44 181 371 1037 | <cl...@demon.net> |
Written on my laptop; please observe the Reply-To address |

0 new messages