Thread: Linked Lists? Wowser...Any suggestions

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    17

    Linked Lists? Wowser...Any suggestions

    Code:
    typedef struct dep_db
    {
        char name[NAME_LENGTH + 1];
        char ssn[SSN_LENGTH + 1];
        struct dep_db* next;
    } DEP_DB_T;
    
    void print_links( const DEP_DB_T* ptr );
    DEP_DB_T* get_links( void);
    
    
    DEP_DB_T* get_links( void )
    {
        DEP_DB_T *current, *first;
        int response;
    
        current = first = malloc( sizeof(DEP_DB_T) );
        printf ("\n\n\tEnter First Item --> ");
        scanf ( "%s", current -> name );
    
        printf ("\n\n\n\tAdd Another? --> 1==y 0==n");
        scanf ("%d", &response);
    
        while (response)
        {
            if ((current->next = malloc( sizeof ( DEP_DB_T ) ) ) == NULL )
            {
                printf ("Youre out of memory");
                return first;
            }
            current = current -> next;
    
            printf ("\n\n\tNAME: ");
            scanf ( "%s", current -> name );
    
            printf ("\n\n\n\tAdd Another? --> 1==y 0==n");
            scanf ("%d", &response);
        }
        current -> next = NULL;
        return first;
    }
    
    void print_links ( const DEP_DB_T* ptr )
    {
        int count = 1;
        printf ("\n\n\n" );
        while ( ptr != NULL)
        {
            printf ("\nnumber %d is %s.", count++, ptr -> name);
            ptr = ptr -> next;
        }
    }
    When I try to compile i get the following

    ANSI C++ forbids implicit conversion from "void *" in assignment?

    I am so confused...

    Please Help!!

    - shane

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try compiling as a C program instead of C++.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    17

    Lightbulb

    I figured it out...

    Needed to explicitly type the return value

    - shane

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by shaeng
    I figured it out...

    Needed to explicitly type the return value

    - shane
    No. You need to do what I said. Stop compiling as C++. This is a common issue posted here. C++ requires you typecast your void pointes. C does not. Compile your program correctly and you'll avoid the problem. It is not recommended you typecast your malloc returns in C.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    17

    Quzah, you were right!

    Quzah,

    thanks for the tip.
    You were right, sometimes a solution that is found is not the best way to go.

    A practice I need to start ingraining in my head.

    thanks

    -shane

  6. #6
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    Originally posted by quzah
    It is not recommended you typecast your malloc returns in C.
    why?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read this for malloc casting.

    Also, do a forum search, this comes up loads of times.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM