Thread: Returning error code in linked list, typecast?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    16

    Returning error code in linked list, typecast?

    I have a strcture called NODE to hold my list and a function NODE find() that looks for an item in the list and returns the NODE. How would I return an error code if the item isn't found? If I just put return -1 or return NULL I get an incompatible type error, and I don't know how to cast to the current type. (NODE *)NULL doesn't work.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    you can only return types you can't return to a specific variable. ex
    Code:
    #include <stdio.h>
    
    struct bob{
            int x;
    };
    
    struct bob func(int y,struct bob test);
    
    int main(void){
            struct bob new;
            new=func(10,new);
            return 0;
    }
    
    struct bob func(int y, struct bob test){
            test.x=y;
            return test;
    }
    I guess you could return all elements of the structure to NULL or its equivalent.
    Last edited by linuxdude; 11-25-2004 at 10:59 PM.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You should be using pointers rather than passing structs around. It is far more efficient and you can return NULL when required. It also prevents problems if the original changes and is the accepted style.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    16
    Sorry, I didn't explain very well.

    Code:
    typedef struct{
     int bob, back, front;
    }NODE; // this is what I meant
    
    typedef struct{
     NODE *list;
     //....
    }LIST;
    
    // ..............
    
    NODE find(LIST l, int find_me){
     //can't find "find_me"
     return /*NULL error code*/; // stuck here
    }

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    pass the pointer to a struct instead of the actual struct like anonytmous said

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    While you're at it, return a NODE pointer instead of a NODE.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. returning a linked list from a function
    By jlshipman in forum C++ Programming
    Replies: 3
    Last Post: 01-23-2006, 07:05 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM