Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closer - Now issue with converting 'void *' to 'MOLECULE *' 1

Status
Not open for further replies.

jamez05

Programmer
Jul 29, 2005
130
US
Thanks everyone for your previous advice/help. Starting to finally grasp the C/C++

Right now I'm trying to convert some C code originally written for Solaris to a windows 32 console program.

In main, I declare MOLECULE *m = NULL;

Another function named initmole uses this variable. The following error happens on initmole.cpp:
Code:
error C2440: '=' : cannot convert from 'void *' to 'MOLECULE *'
I'm still not really following what malloc does, but it seems like it has to do with this error because it errors on that line.
Here's the code for initmole.cpp:
Code:
/*==========================================================================*/
/*                                 InitMol()                                */
/*                                                                          */
/*  Create a molecule connection table and initialize its entries.          */
/*                                                                          */
/*  Argument list:     void                                                 */
/*                                                                          */
/*  Return value:      MOLECULE *       pointer to created connection table */
/*                                                                          */
/*==========================================================================*/

#include "stdafx.h"
#include <stddef.h>
#include <stdlib.h>
#include "molecule.h"


MOLECULE *InitMol( void )
{
   MOLECULE *m;
  
   if ( (m = malloc( sizeof( *m ) )) == NULL ) {
	  return( NULL );
   }

   m->N_Hydro = -1;
   m->N_Atoms =  0;
   m->N_Bonds =  0;
   m->N_Comps =  0;
   return( m );
}

Does anyone know what's going on or can give me some advice on how to handle this?

Thanks

James
 
Found out that I may need to cast the m variable. I'm guessing the cast would be something like:
Code:
if ( (m[Data_Type] = malloc( sizeof( *m ) )) == NULL )

However, I'm not sure what the data type is because its declared as:
Code:
MOLECULE *m = NULL;
 
I just tried the following (which seems closer), but I'm getting the error "error C2440: '=' : cannot convert from 'bool' to 'MOLECULE *'":

Code:
(m = (MOLECULE*)malloc( sizeof( *m )
 
Figured it out:

Code:
 (m = (MOLECULE*)malloc( sizeof( *m ) )) == NULL
 
or even better (since this is C++ now):
Code:
MOLECULE *InitMol()
{
   MOLECULE* m = NULL;

   try
   {
      m = new MOLECULE;
      m->N_Hydro = -1;
      m->N_Atoms =  0;
      m->N_Bonds =  0;
      m->N_Comps =  0;
   }
   catch (...)
   {
      // Maybe print an error or something.
   }

   return m;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top