Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help using a struct

  1. Jun 11, 2005 #1
    Could someone tell me what it wrong with this code? It seems to be identical to the code in a book i'm using but I get the error message with the line i've colored red:

    error C2440: '=' : cannot convert from 'void *' to 'struct main::node *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast

    int main()
    {

    struct node{
    int data;
    struct node *nextPtr;
    };

    struct node newPtr;
    newPtr = malloc( sizeof( struct node ) );

    return 0;
    }

    edit:After including <stdlib.h> which I realized I need for the memory allocation, I get a new error message:

    error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'void *' (or there is no acceptable conversion)

    I'm using Visual C++ 6.0 if that makes any difference
     
    Last edited: Jun 11, 2005
  2. jcsd
  3. Jun 11, 2005 #2
    int main()
    {
    struct node{
    int data;
    struct node *nextPtr;
    };

    struct node *newPtr;
    newPtr = (node*)malloc( sizeof( struct node ) );

    return 0;
    }

    -- AI
     
  4. Jun 11, 2005 #3

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    This statement already allocates space for a "struct node".

    This statement allocates memory, and returns a pointer to the allocated memory memory. (a void pointer, specifically)

    This variable is not a pointer, thus you cannot assign to it a memory address address.

    In (ANSI) C, node isn't a type, so this should fail. You meant "(struct node*)", but I know you knew that. :smile:
     
  5. Jun 11, 2005 #4
    Ok, apparently the code i'm trying to compile does not work in C++. I found the CD for the book i'm using, and opened up the source code. It opened up as a .C file and worked fine. However, when I created a new source file (in Visual C++) it created a .cpp file. I copied over the code and I got the error I originally posted. I don't understand why this won't compile as a .cpp file. I thought C was essentially a subclass of C++. Here's the code in it's entirety:

    /* Fig. 12.3: fig12_03.c
    Operating and maintaining a list */
    #include <stdio.h>
    #include <stdlib.h>

    struct listNode { /* self-referential structure */
    char data;
    struct listNode *nextPtr;
    };

    typedef struct listNode ListNode;
    typedef ListNode *ListNodePtr;

    void insert( ListNodePtr *, char );
    char delete( ListNodePtr *, char );
    int isEmpty( ListNodePtr );
    void printList( ListNodePtr );
    void instructions( void );

    int main()
    {
    ListNodePtr startPtr = NULL;
    int choice;
    char item;

    instructions(); /* display the menu */
    printf( "? " );
    scanf( "%d", &choice );

    while ( choice != 3 ) {

    switch ( choice ) {
    case 1:
    printf( "Enter a character: " );
    scanf( "\n%c", &item );
    insert( &startPtr, item );
    printList( startPtr );
    break;
    case 2:
    if ( !isEmpty( startPtr ) ) {
    printf( "Enter character to be deleted: " );
    scanf( "\n%c", &item );

    if ( delete( &startPtr, item ) ) {
    printf( "%c deleted.\n", item );
    printList( startPtr );
    }
    else
    printf( "%c not found.\n\n", item );
    }
    else
    printf( "List is empty.\n\n" );

    break;
    default:
    printf( "Invalid choice.\n\n" );
    instructions();
    break;
    }

    printf( "? " );
    scanf( "%d", &choice );
    }

    printf( "End of run.\n" );
    return 0;
    }

    /* Print the instructions */
    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" );
    }

    /* Insert a new value into the list in sorted order */
    void insert( ListNodePtr *sPtr, char value )
    {
    ListNodePtr newPtr, previousPtr, currentPtr;

    newPtr = malloc( sizeof( ListNode ) );

    if ( newPtr != NULL ) { /* is space available */
    newPtr->data = value;
    newPtr->nextPtr = NULL;

    previousPtr = NULL;
    currentPtr = *sPtr;

    while ( currentPtr != NULL && value > currentPtr->data ) {
    previousPtr = currentPtr; /* walk to ... */
    currentPtr = currentPtr->nextPtr; /* ... next node */
    }

    if ( previousPtr == NULL ) {
    newPtr->nextPtr = *sPtr;
    *sPtr = newPtr;
    }
    else {
    previousPtr->nextPtr = newPtr;
    newPtr->nextPtr = currentPtr;
    }
    }
    else
    printf( "%c not inserted. No memory available.\n", value );
    }

    /* Delete a list element */
    char delete( ListNodePtr *sPtr, char value )
    {
    ListNodePtr previousPtr, currentPtr, tempPtr;

    if ( value == ( *sPtr )->data ) {
    tempPtr = *sPtr;
    *sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
    free( tempPtr ); /* free the de-threaded node */
    return value;
    }
    else {
    previousPtr = *sPtr;
    currentPtr = ( *sPtr )->nextPtr;

    while ( currentPtr != NULL && currentPtr->data != value ) {
    previousPtr = currentPtr; /* walk to ... */
    currentPtr = currentPtr->nextPtr; /* ... next node */
    }

    if ( currentPtr != NULL ) {
    tempPtr = currentPtr;
    previousPtr->nextPtr = currentPtr->nextPtr;
    free( tempPtr );
    return value;
    }
    }

    return '\0';
    }

    /* Return 1 if the list is empty, 0 otherwise */
    int isEmpty( ListNodePtr sPtr )
    {
    return sPtr == NULL;
    }

    /* Print the list */
    void printList( ListNodePtr currentPtr )
    {
    if ( currentPtr == NULL )
    printf( "List is empty.\n\n" );
    else {
    printf( "The list is:\n" );

    while ( currentPtr != NULL ) {
    printf( "%c --> ", currentPtr->data );
    currentPtr = currentPtr->nextPtr;
    }

    printf( "NULL\n\n" );
    }
    }

    Can anyone explain why this won't compile in C++?
     
    Last edited: Jun 11, 2005
  6. Jun 11, 2005 #5
    Oops!! I am used to much of 'typedef'ing that it just came out of habit. :biggrin:

    Single word "typecast".

    -- AI
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?