Thread: error C2065: 'NULL'

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

    error C2065: 'NULL'

    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 );
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    malloc.h is archaic. Use stdlib.h instead. You also don't need to cast the return of malloc.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    Thanks Prelude,

    That solved the problem:

    new code:
    Code:
    #include <stdlib.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 );
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if ( (m = (MOLECULE *) sizeof( *m ) ) == NULL )
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    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.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    if ( (m = (MOLECULE *) sizeof( *m ) ) == NULL ) {
    Presumably you meant
    Code:
    if ( (m = (MOLECULE *)malloc( sizeof( *m ) )) == NULL ) {
    in which case you should read the FAQ and then change it to this:
    Code:
    if ( (m = malloc( sizeof( *m ) )) == NULL ) {
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-14-2004, 11:06 PM
  5. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM