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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error C2065: Undeclared Identifier

Status
Not open for further replies.

jamez05

Programmer
Jul 29, 2005
130
0
0
US
I'm using Visual Studio 2005 on Windows XP. I'm trying to compile a C program. I'm down to one error. When I try to compile the following, I get the error:

initmol.c(20) : error C2065: 'NULL' : undeclared identifier

Here's the code. It errors out on the IF statement:

Code:
#include <malloc.h>
#include "molecule.h"

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

   m->N_Hydro = -1;
   m->N_Atoms =  0;
   m->N_Bonds =  0;
   m->N_Comps =  0;
   return( m );
}
 
I guess malloc.h doesn't define NULL. Try including <stddef.h>
 
Thanks cpjust.

I ended up replacing malloc.h with stdio.h and got rid of the malloc cast and it solved the problem:

Code:
#include <stdio.h>
#include "molecule.h"

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

   m->N_Hydro = -1;
   m->N_Atoms =  0;
   m->N_Bonds =  0;
   m->N_Comps =  0;
   return( m );
}
 
Well now you aren't allocating any memory at all. You're just setting the pointer to the size of the MOLECULE struct. This will blow up when you run it.
 
Thanks cpjust, that's exactly what's happening. I will
change it back and add <stddef.h>. I'll let you know what happens
 
The NULL macro and malloc func are both declared in <stdlib.h>.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top