Thread: C-style Cast Problem

  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
    28,413
    I think it might be:
    Code:
    m = (MOLECULE*)malloc( sizeof( *m ) ) == NULL
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Oops, I did not properly modify your code snippet. It should be:
    Code:
    if ( (m = (MOLECULE*)malloc( sizeof( *m ) )) == NULL ) {
        return( NULL );
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

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, 10:14 PM
  5. Microsoft Visual C++ compiler, cast problem?
    By jonnie75 in forum C Programming
    Replies: 5
    Last Post: 11-10-2001, 08:53 AM