By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

MSVC type cast warning

P: n/a
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.

Mar 15 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

--
Er*********@sun.com
Mar 15 '07 #2

P: n/a
llothar wrote:
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.

--
Chris "electric hedgehog" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Mar 15 '07 #3

P: n/a
On 15 Mrz., 06:59, Chris Dollin <chris.dol...@hp.comwrote:
llothar wrote:
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.

--
Chris "electric hedgehog" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

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.
Mar 15 '07 #4

P: n/a
"llothar" <ll*****@web.dewrites:
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.
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
Mar 15 '07 #5

P: n/a
On Mar 15, 5:28 pm, "llothar" <llot...@web.dewrote:
On 15 Mrz., 06:59, Chris Dollin <chris.dol...@hp.comwrote:
<snip>
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.

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.
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.
--
WYCIWYG - what you C is what you get

Mar 15 '07 #6

P: n/a
Ben Pfaff wrote, On 15/03/07 16:39:
"llothar" <ll*****@web.dewrites:
>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.
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.
--
Flash Gordon.
Mar 15 '07 #7

P: n/a
llothar wrote:
On 15 Mrz., 06:59, Chris Dollin <chris.dol...@hp.comwrote:
>llothar wrote:
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.

--
Chris "electric hedgehog" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/
(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).

--
Chris "shuddering at the image" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Mar 16 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.