Why am I getting this error?

This is a discussion on Why am I getting this error? within the C Programming forums, part of the General Programming Boards category; I keep getting this error (I'm using Visual C++ 2005 Express Edition by the way): error C2440: '=' : cannot ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    28

    Why am I getting this error?

    I keep getting this error (I'm using Visual C++ 2005 Express Edition by the way):

    error C2440: '=' : cannot convert from 'void *' to 'ListNodePtr'



    Code:
    /* Operating and maintaining a list */
    #include<stdio.h>
    #include<stdlib.h>
    
    /* self-referential structure */
    struct listNode {            
       char data; /* each listNode contains a character */
       struct listNode *nextPtr; /* pointer to next node*/ 
    }; /* end structure listNode */
    
    typedef struct listNode ListNode; /* synonym for struct listNode */
    typedef struct listNode *ListNodePtr; /* synonym for ListNode* */
    
    /* prototypes */
    void insert( ListNodePtr *sPtr, char value );
    char del( ListNodePtr *sPtr, char value );
    int isEmpty( ListNodePtr sPtr );
    void printList( ListNodePtr currentPtr );
    void instructions( void );
    
    int main( void )
    { 
       ListNodePtr startPtr = NULL; /* initially there are no nodes */
       int choice; /* user's choice */
       char item;  /* char entered by user */
    
       instructions(); /* display the menu */
       printf("? ");
       scanf_s("%d", &choice);
    
       /* loop while user does not choose 3 */
       while ( choice != 3 ) { 
    
          switch ( choice ) { 
    
             case 1:
                printf( "Enter a character: " );
                scanf_s( "\n%c", &item );
                insert( &startPtr, item ); /* insert item in list */
                printList( startPtr );
                break;
    
             case 2:
    
                /* if list is not empty */
                if ( !isEmpty( startPtr ) ) { 
                   printf( "Enter character to be deleted: " );
                   scanf_s( "\n%c", &item );
    
                   /* if character is found, remove it */
                   if ( del( &startPtr, item ) ) { /* remove item */
                      printf( "%c deleted.\n", item );
                      printList( startPtr );
                   } /* end if */
                   else {
                      printf( "%c not found.\n\n", item );
                   } /* end else */
    
                } /* end if */
                else {
                   printf( "List is empty.\n\n" );
                } /* end else */
    
                break;
    
             default:
                printf( "Invalid choice.\n\n" );
                instructions();
                break;
          
          } /* end switch */
    
          printf( "? " );
          scanf_s( "%d", &choice );
       } /* end while */
    
       printf( "End of run.\n" );
       
       return 0; /* indicates successful termination */
    
    } /* end main */
    
    /* display program instructions to user */
    void instructions( void )
    { 
       printf( "Enter your choice:\n"
          "   1 to insert an element into the list.\n"
          "   2 to delete an element from the list.\n"
          "   3 to end.\n" );
    } /* end function instructions */
    
    /* Insert a new value into the list in sorted order */
    void insert( ListNodePtr *sPtr, char value )
    { 
       ListNodePtr newPtr;      /* pointer to new node */
       ListNodePtr previousPtr; /* pointer to previous node in list */
       ListNodePtr currentPtr;  /* pointer to current node in list */
    
       newPtr = malloc( sizeof(ListNode) ); /* create node */  <- this is where I start getting the error
    
       if ( newPtr != NULL ) { /* is space available */
          newPtr->data = value; /* place value in node */
          newPtr->nextPtr = NULL; /* node does not link to another node */
    
          previousPtr = NULL;
          currentPtr = *sPtr;
    
          /* loop to find the correct location in the list */
          while ( currentPtr != NULL && value > currentPtr->data ) { 
             previousPtr = currentPtr;          /* walk to ...   */
             currentPtr = currentPtr->nextPtr;  /* ... next node */
          } /* end while */
    
          /* insert new node at beginning of list */
          if ( previousPtr == NULL ) { 
             newPtr->nextPtr = *sPtr;
             *sPtr = newPtr;
          } /* end if */
          else { /* insert new node between previousPtr and currentPtr */
             previousPtr->nextPtr = newPtr;
             newPtr->nextPtr = currentPtr;
          } /* end else */
       
       } /* end if */
       else {
          printf( "%c not inserted. No memory available.\n", value );
       } /* end else */
    
    } /* end function insert */
    
    /* Delete a list element */
    char del( ListNodePtr *sPtr, char value )
    { 
       ListNodePtr previousPtr; /* pointer to previous node in list */
       ListNodePtr currentPtr;  /* pointer to current node in list */
       ListNodePtr tempPtr;     /* temporary node pointer */
    
       /* delete first node */
       if ( value == ( *sPtr )->data ) { 
          tempPtr = *sPtr; /* hold onto node being removed */
          *sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
          free( tempPtr ); /* free the de-threaded node */
          return value;
       } /* end if */
       else { 
          previousPtr = *sPtr;
          currentPtr = ( *sPtr )->nextPtr;
    
          /* loop to find the correct location in the list */
          while ( currentPtr != NULL && currentPtr->data != value ) { 
             previousPtr = currentPtr;         /* walk to ...   */
             currentPtr = currentPtr->nextPtr; /* ... next node */  
          } /* end while */
    
          /* delete node at currentPtr */
          if ( currentPtr != NULL ) { 
             tempPtr = currentPtr;
             previousPtr->nextPtr = currentPtr->nextPtr;
             free( tempPtr );
             return value;
          } /* end if */
         
       } /* end else */
    
       return '\0';
    
    } /* end function delete */
    
    /* Return 1 if the list is empty, 0 otherwise */
    int isEmpty( ListNodePtr sPtr )
    { 
       return sPtr == NULL;
    
    } /* end function isEmpty */
    
    /* Print the list */
    void printList( ListNodePtr currentPtr )
    { 
    
       /* if list is empty */
       if ( currentPtr == NULL ) {
          printf( "List is empty.\n\n" );
       } /* end if */
       else { 
          printf( "The list is:\n" );
    
          /* while not the end of the list */
          while ( currentPtr != NULL ) { 
             printf( "%c --> ", currentPtr->data );
             currentPtr = currentPtr->nextPtr;   
          } /* end while */
    
          printf( "NULL\n\n" );
       } /* end else */
    
    } /* end function printList */
    Last edited by We'reGeeks; 01-02-2011 at 02:14 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    It looks like you are compiling as C++. Change the source file extension to .c and configure the project settings to compile as C.
    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
    Nov 2010
    Posts
    28
    Why? When I ran my other C programs, I didn't have to specify their extension as ".c". They simply ran. =/
    Also, there is only one error in the program I posted. =/
    Last edited by We'reGeeks; 01-02-2011 at 02:44 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by We'reGeeks View Post
    Why? When I ran my other C programs, I didn't have to specify their extension as ".c". They simply ran. =/
    Also, there is only one error in the program I posted. =/
    Whiteflags is probably correct.

    C and C++ are bundled together in Visual C++ downloads but in no way are they the same language. C++ is not merely an extension to C... there are differences in syntax and behaviour that can and do matter.

    Just because you've written a couple of programs that didn't land on any of the differences does not mean the differences aren't there...

    Try Whitflags advice... switch the compiler type to C for your project and change the file extension to .c. See what happens...

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Alright. You're right. I finally got it! Yay! Thanks much. ^^,
    Last edited by We'reGeeks; 01-02-2011 at 06:11 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    Quote Originally Posted by We'reGeeks
    Also, there is only one error in the program I posted. =/
    It is precisely the nature of that error that allowed me to deduce that you are compiling the code as C++

    In C, an implicit conversion from void* (i.e., the return type of malloc) to ListNodePtr is allowed. In C++, it requires an explicit cast.
    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

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by We'reGeeks View Post
    Alright. You're right. I finally got it! Yay! Thanks much. ^^,
    Bet you don't get caught on that one again... LOL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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