C-style Cast Problem

This is a discussion on C-style Cast Problem within the C++ Programming forums, part of the General Programming Boards category; I'm converting a C program written for Solaris to a Windows 32 console program using Visual Studio 2005. In my ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    80

    C-style Cast Problem

    I'm converting a C program written for Solaris to a Windows 32 console program using Visual Studio 2005.

    In my t_main function (found in test1.cpp), the following is declared:
    Code:
    MOLECULE *m = NULL;
    When I try to compile, I get the following error in my initmole.cpp page:
    Code:
     error C2440: '=' : cannot convert from 'int *' to 'MOLECULE *'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    I'm pretty sure that I have to perform a cast on molecule *m in initmole.cpp. However, m is an array/struct so I have no idea on how to cast it. Below is
    the code for initmole.cpp. How/where would I want to perform a cas?
    Code:
    MOLECULE *InitMol( void )
    {
       MOLECULE *m;
      
       if ( (m = malloc( sizeof( *m ) )) == NULL ) { //this where it errors out
    	  return( NULL );
       }
    
       m->N_Hydro = -1;
       m->N_Atoms =  0;
       m->N_Bonds =  0;
       m->N_Comps =  0;
       return( m );
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    I think it might be:
    Code:
    m = (MOLECULE*)malloc( sizeof( *m ) ) == NULL
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    Thank you laserlight for your reply.

    I tried your suggestion, however now its giving the error:
    Code:
    error C2440: '=' : cannot convert from 'bool' to 'MOLECULE *'

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Oops, I did not properly modify your code snippet. It should be:
    Code:
    if ( (m = (MOLECULE*)malloc( sizeof( *m ) )) == NULL ) {
        return( NULL );
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    Thank you so much, it worked!!! I'll study it and try to get a grasp on what that actually did.

    Have a nice day

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Well if you're really converting it to C++ (as opposed to merely accepting the filenames VC++ gives you and pretending that it's still C), then you should be converting all your malloc/free calls into new/delete.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. problem converting int to char with or without cast?
    By skuzzy in forum C Programming
    Replies: 5
    Last Post: 10-10-2005, 07:07 PM
  3. problem with data cast
    By davide_82 in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-28-2005, 12:44 AM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 09:14 PM
  5. Microsoft Visual C++ compiler, cast problem?
    By jonnie75 in forum C Programming
    Replies: 5
    Last Post: 11-10-2001, 07:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21