MSVC type cast warning

L

llothar

When i use -W4 on visual c 7.0 i get warning C4054

translator1.c(1703) : warning C4054: 'type cast' : from function
pointer 'void * (__cdecl *)(se_agent *)' to data pointer 'void *'
translator1.c(1703) : warning C4152: nonstandard extension, function/
data pointer conversion in expression

whenever i cast a function pointer to a void* or back.
Is there any reason for this warning ? Looks like good C code for me.
 
C

Chris Dollin

llothar said:
When i use -W4 on visual c 7.0 i get warning C4054

translator1.c(1703) : warning C4054: 'type cast' : from function
pointer 'void * (__cdecl *)(se_agent *)' to data pointer 'void *'
translator1.c(1703) : warning C4152: nonstandard extension, function/
data pointer conversion in expression

whenever i cast a function pointer to a void* or back.
Is there any reason for this warning ? Looks like good C code for me.

It isn't. Function pointers and object pointers in C are not interchangable.
 
E

Eric Sosman

llothar wrote On 03/15/07 10:33,:
When i use -W4 on visual c 7.0 i get warning C4054

translator1.c(1703) : warning C4054: 'type cast' : from function
pointer 'void * (__cdecl *)(se_agent *)' to data pointer 'void *'
translator1.c(1703) : warning C4152: nonstandard extension, function/
data pointer conversion in expression

whenever i cast a function pointer to a void* or back.
Is there any reason for this warning ? Looks like good C code for me.

Do you think it might have been a smart move to show
us the actual "good C code?" Hmmmm?

Fortunately, there's enough information in the error
messages -- in this case! -- to allow me to guess what you
have done wrong. You have tried to convert a function
pointer to a data pointer, and that's a no-no. Code is
code and data is data and never the twain shall meet.
 
L

llothar

It isn't. Function pointers and object pointers in C are not interchangable.


Okay, i found the explaination for this under "Harvard Architecture"
on wikipedia.
But what's with function arguments casting? Is it leagal to do cast
two methods


typedef void (*one_param_func_t)(void* param1);
typedef void (*two_param_func_t)(void* param1, void* param2);

void test()
{
one_param_func_t func_with_two_params;
two_param_func_t func_with_one_param;

/* Question ? */
func_with_two_params = func_with_one_param;
};

Otherwise it would be extremely difficult to provide a ANSI-C eiffel
generator.
 
B

Ben Pfaff

llothar said:
But what's with function arguments casting? Is it leagal to do cast
two methods


typedef void (*one_param_func_t)(void* param1);
typedef void (*two_param_func_t)(void* param1, void* param2);

void test()
{
one_param_func_t func_with_two_params;
two_param_func_t func_with_one_param;

/* Question ? */
func_with_two_params = func_with_one_param;
};

You can do that if you use a cast; that is, there's no implicit
conversion, but you can request one explicitly.

Actually calling a function through a function pointer of the
wrong type, though, yields undefined behavior.
 
F

Flash Gordon

Ben Pfaff wrote, On 15/03/07 16:39:
You can do that if you use a cast; that is, there's no implicit
conversion, but you can request one explicitly.

Actually calling a function through a function pointer of the
wrong type, though, yields undefined behavior.

If, on the other hand, you later cast it back to the correct type, you
are guaranteed that it will work.

Ben, of course, knows this, the comment is for the OP.
 
M

matevzb

It isn't. Function pointers and object pointers in C are not interchangable.

Okay, i found the explaination for this under "Harvard Architecture"
on wikipedia.
But what's with function arguments casting? Is it leagal to do cast
two methods

typedef void (*one_param_func_t)(void* param1);
typedef void (*two_param_func_t)(void* param1, void* param2);

void test()
{
one_param_func_t func_with_two_params;
two_param_func_t func_with_one_param;

/* Question ? */
func_with_two_params = func_with_one_param;

};

Otherwise it would be extremely difficult to provide a ANSI-C eiffel
generator.[/QUOTE]
Although you cannot interchange object and function pointers in
standard C, this is a (very) common extension, listed in the Standard
even. To do what you want portably though, read question 4.13 in
comp.lang.c FAQ.
 
C

Chris Dollin

(you're supposed to snip signatures: I left mine in here to show
what it was you shouldn't do ...)
But what's with function arguments casting?

You mean function /pointer/ casting.
Is it leagal to do cast two methods

(fx:snip)

So long as when you call a function through a pointer of type PF
that function really is of type PF, you're OK. You /have/ to cast
to convert a function pointer from one type to another.
Otherwise it would be extremely difficult to provide a ANSI-C eiffel
generator.

Perhaps. But such a compiler could be documented to rely on features
outside the ANSI standard, if they were widely available.

I have an written-in-C interpreter for a programming language which
we'll call X for the sake of convenience. In it's current state,
the interpreter assumes that there's an integer type T of at least
32 bits to which pointers to a structy type can be freely cast and
recovered, and what's more that there's room in that pointer and
integer for two "spare" bits.

The code won't port to a target where that's not true. [It would be
an interesting exercise to work out how it could be revised to remove
that assumption without loss of efficiency.] Fortunately I didn't
expect to port to any such target (and the code is essentially
dead now, although it may still bear children).
 

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

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top