473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

MSVC type cast warning

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
7 5220
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: icedac | last post by:
I have some questions about template and c++ itself. ///////////////////////////////////////////////////////////////////////// q1) see follow c++ code, and compile. it's works only IntelC++8.1...
15
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int)...
25
by: hugo2 | last post by:
Obrhy/hugo July 12, 2004 Take a look at this memcpy() definition. Is there a good reason the void pointer args are cast to byte just to assign their addresses to byte pointers? /*from Steve...
16
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
12
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ...
2
by: BruceWho | last post by:
I downloaded boost1.35.0 and built it with following command: bjam --toolset=msvc-7.1 --variant=release --threading=multi -- link=shared --with-system stage and it failed to compile, error...
3
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
4
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.