Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Do we need casting to match void * to char * (http://www.programmingforums.org/showthread.php?t=14894)

kurt Jan 8th, 2008 12:32 PM

Do we need casting to match void * to char *
 
Why is it that the compiler complains "invalid conversion from void * to char*" when I do not cast it? Isn't void* supposed to be generic?

I have a bunch of files from another person which I am not allowed to edit, and he/she does not cast them. He/she argues that they are compatible without casting and it should work.

Is it a compiler specific thing, where I should be setting some flags for it?

Thanks in advance, everyone.

Kurt

Narue Jan 8th, 2008 1:41 PM

Re: Do we need casting to match void * to char *
 
>Isn't void* supposed to be generic?
Yes, but that doesn't mean C++ allows an automatic conversion like C. You still have to cast it.

kurt Jan 8th, 2008 3:52 PM

Re: Do we need casting to match void * to char *
 
Quote:

Originally Posted by Narue (Post 139339)
>Isn't void* supposed to be generic?
Yes, but that doesn't mean C++ allows an automatic conversion like C. You still have to cast it.

Hm, I guess I can accept casting is need.

But are there any compiler flags that can solve this? I've check the compiler manual, no such thing. So I'm wondering if there are common practice that does not cast it but still manage to compile.

Narue Jan 8th, 2008 5:49 PM

Re: Do we need casting to match void * to char *
 
>But are there any compiler flags that can solve this?
Um, there's nothing to solve. Type safety is a fundamental feature of C++.

Salem Jan 8th, 2008 6:55 PM

Re: Do we need casting to match void * to char *
 
> But are there any compiler flags that can solve this?
You mean like use a C compiler to compile C code perhaps?

> I have a bunch of files from another person which I am not allowed to edit
Why not?
If they're really meant to be C++, then they're clearly wrong.

lectricpharaoh Jan 8th, 2008 7:08 PM

Re: Do we need casting to match void * to char *
 
Quote:

Originally Posted by kurt
I have a bunch of files from another person which I am not allowed to edit, and he/she does not cast them. He/she argues that they are compatible without casting and it should work.

If this is on the job, then you should point out it's broken code. If you or the employer has a copy of the C++ standard, or even any reputable C++ books, you should be able to make this case.

If this is an instructor who has given you these files, and forbidden you to edit them, then you should slap them upside the head with a wet fish, and seek another school.

kurt Jan 8th, 2008 7:48 PM

Re: Do we need casting to match void * to char *
 
Quote:

Originally Posted by lectricpharaoh (Post 139356)
If this is on the job, then you should point out it's broken code. If you or the employer has a copy of the C++ standard, or even any reputable C++ books, you should be able to make this case.

If this is an instructor who has given you these files, and forbidden you to edit them, then you should slap them upside the head with a wet fish, and seek another school.

Haha. A good one :icon_cheesygrin:

Anyway, you guys are right. It was meant for C, and using the C instead of C++ compiler worked.

Thanks guys!


All times are GMT -5. The time now is 3:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC