Thread: C2440 Error message?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    38

    C2440 Error message?

    Does anyone know what this error message means:

    error C2440: '=' : cannot convert from 'void *' to 'char *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast...

    The code which causes this error is:

    Code:
    tables[0] = malloc(strlen((table) +1) * sizeof(char));
    Many thanks
    Simon

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It means you're compiling C code with a C++ compiler.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    Is there a way to get round it, I thought C code was compatible 99% more or less in C++ envionment....

  4. #4
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    change extension .cpp to .c

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I thought C code was compatible 99% more or less in C++ envionment
    Congratulations, you found part of that 1% that's not compatible. If you're compiling C code you should use a C compiler. If you're compiling C++ code you should use a C++ compiler.
    If you understand what you're doing, you're not learning anything.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Is there a way to get round it<<

    You could tell your compiler to compile as C (/TC) regardless of the file extension of source files.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    That sounds like a more sensible solution Ken - in the C/C++ tab in Property-> Settings, I cant find the advanced tab to tell VS to compile as C.
    Do you know where it is?

    Thanks

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    Actually, the example link you have provided refers to Visual Studio .NET, how do you set to compile C for version 6 of Visual Studio??

    Can anyone help, it cant be that hard, although I still cant find the damn option

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    How about just casting the void * returned by malloc() to type char * using the C casting syntax, like this:


    tables[0] = (char *)malloc(strlen((table) +1) * sizeof(char));

    Assuming tables is an array of char * that might work.
    You're only born perfect.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Just name your files properly.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    another solution that might work (haven't tested)
    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    tables[0] = malloc(strlen((table) +1) ); /* removed sizeof(char) since it will always be 1 */
    #ifdef __cplusplus
    }
    #endif
    Last edited by Thantos; 01-05-2005 at 02:30 PM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    I feel ill.....

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Its ok salem, the evil C++ will leave eventually

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by earnshaw
    That sounds like a more sensible solution Ken - in the C/C++ tab in Property-> Settings, I cant find the advanced tab to tell VS to compile as C.
    Do you know where it is?

    Thanks
    How is this more sensible than renaming your file? .C means C. .CPP means C++. What is not to understand here? Do what your compiler expects you to, and you won't have to jump through hoops to work around your intentional stupidity. And yes, at this point you're simply being stupid, because you've already been told what the problem is, so you cannot claim ignorance. You still refuse to use the correct resolution, preferring to concoct some crazy work around, rather than simply doing as you should. That's just being stupid.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by Thantos
    another solution that might work (haven't tested)
    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    tables[0] = malloc(strlen((table) +1) ); /* removed sizeof(char) since it will always be 1 */
    #ifdef __cplusplus
    }
    #endif
    extern "C" turns off name mangling, and by implication features which use name mangling such as function overloading. This allows extern "C" functions to be called from C, other programming languages and other compilers. The code is still compiled as C++. extern "C" can be thought of as a linker instruction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error C2440: 'type cast' HELP!!
    By Brownie in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2008, 08:54 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Need help with error C2440
    By licobob in forum C Programming
    Replies: 1
    Last Post: 05-02-2005, 06:57 AM
  4. Help with error: C2440
    By dogman in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 04:56 PM
  5. Replies: 6
    Last Post: 04-14-2002, 11:25 AM