Return type

Discussion in 'C Programming' started by danieljmathew@gmail.com, Aug 25, 2005.

  1. Guest

    Problem:

    I want to write a function for retrieving a value from a linked list,
    given the position as an int.
    Function prototype: int retrieve(int pos, List *l);
    There are 2 abnormal conditions - the position given is greater than
    the size of the list, or the list is empty. I thought of returning 0 in
    case these errors occur.
    But there arises the problem - even if there's no error, zero can be
    returned (if that's the value of the node a the position pos).

    One solution i conjured is:
    Change the function to :
    int retrieve(int *pos, List *l);
    Now pos is passed by reference. If there's an error the function
    returns 0. Otherwise it stores the value of the node at position *pos
    to the int *pos, and returns 1.
    So the calling function first checks whether the return value is 1, and
    if so, it prints the value pointed by first argument pos (or whatever
    name it has in the calling function):
    int pos=14;
    List ls;
    if(retrieve(&pos,&ls)) printf("%d",pos);

    Is there any other, more elegant solution?
    Dany
    , Aug 25, 2005
    #1
    1. Advertising

  2. Steffen Guest

    Hi

    > I want to write a function for retrieving a value from a linked list,
    > given the position as an int.
    > Function prototype: int retrieve(int pos, List *l);
    > There are 2 abnormal conditions - the position given is greater than
    > the size of the list, or the list is empty. I thought of returning 0 in
    > case these errors occur.
    > But there arises the problem - even if there's no error, zero can be
    > returned (if that's the value of the node a the position pos).
    >
    > One solution i conjured is:
    > Change the function to :
    > int retrieve(int *pos, List *l);
    > Now pos is passed by reference. If there's an error the function
    > returns 0. Otherwise it stores the value of the node at position *pos
    > to the int *pos, and returns 1.


    I think what you did in principle is the only way to deal with an error,
    however i would strongly suggest >not< to use the pos-argument to return
    the result, but rater add another argument, either
    - a pointer to a boolean that indicates whether an error occured or not,

    and simply return the result the usual way
    int retrieve(int pos, List *l, int *ok)
    - a pointer to where the result is to be placed and the return value
    indicating an error
    int retrieve(int pos, List *l, int *result)

    Steffen
    Steffen, Aug 25, 2005
    #2
    1. Advertising

  3. CBFalconer Guest

    "" wrote:
    >
    > Problem:
    >
    > I want to write a function for retrieving a value from a linked list,
    > given the position as an int.
    > Function prototype: int retrieve(int pos, List *l);
    > There are 2 abnormal conditions - the position given is greater than
    > the size of the list, or the list is empty. I thought of returning 0 in
    > case these errors occur.
    > But there arises the problem - even if there's no error, zero can be
    > returned (if that's the value of the node a the position pos).


    Simply return a pointer to the list item. You then retrieve the
    value by dereferencing the pointer. If the position does not exist
    return null.

    list *p;

    p = retrieve(int pos, list *l)
    if (p) value = p->datavalue;
    else /* no such entry */;

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Aug 25, 2005
    #3
  4. akarl Guest

    wrote:
    > Problem:
    >
    > I want to write a function for retrieving a value from a linked list,
    > given the position as an int.
    > Function prototype: int retrieve(int pos, List *l);
    > There are 2 abnormal conditions - the position given is greater than
    > the size of the list, or the list is empty. I thought of returning 0 in
    > case these errors occur.
    > But there arises the problem - even if there's no error, zero can be
    > returned (if that's the value of the node a the position pos).
    >
    > One solution i conjured is:
    > Change the function to :
    > int retrieve(int *pos, List *l);
    > Now pos is passed by reference. If there's an error the function
    > returns 0. Otherwise it stores the value of the node at position *pos
    > to the int *pos, and returns 1.
    > So the calling function first checks whether the return value is 1, and
    > if so, it prints the value pointed by first argument pos (or whatever
    > name it has in the calling function):
    > int pos=14;
    > List ls;
    > if(retrieve(&pos,&ls)) printf("%d",pos);
    >
    > Is there any other, more elegant solution?


    I would do something like

    /* Retrieves element at pos in list.
    Precondition: (pos >= 0) && (pos < count(list)) */

    int retrieve(int pos, List *list)
    {
    assert((pos >= 0) && (pos < list->count));
    ...
    }

    where count(list) returns list->count.


    August
    akarl, Aug 25, 2005
    #4
  5. Default User Guest

    wrote:

    > Problem:
    >
    > I want to write a function for retrieving a value from a linked list,
    > given the position as an int.
    > Function prototype: int retrieve(int pos, List *l);
    > There are 2 abnormal conditions - the position given is greater than
    > the size of the list, or the list is empty. I thought of returning 0
    > in case these errors occur.
    > But there arises the problem - even if there's no error, zero can be
    > returned (if that's the value of the node a the position pos).



    What about -1?



    Brian
    Default User, Aug 25, 2005
    #5
  6. Guest

    Thanx everybody.

    I think i found a solution fitting my requirements in CBF's suggestion.
    I'll return with more newbie doubts if any.

    Dany.
    , Aug 25, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaKoadMunky
    Replies:
    8
    Views:
    3,299
    Leor Zolman
    May 14, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    800
    Keith Thompson
    Mar 6, 2005
  3. gk245
    Replies:
    2
    Views:
    1,189
    Christopher Benson-Manica
    May 6, 2006
  4. Diego Martins
    Replies:
    5
    Views:
    477
    Diego Martins
    Sep 5, 2006
  5. andrew cooke
    Replies:
    1
    Views:
    180
    Steven D'Aprano
    Apr 3, 2011
Loading...

Share This Page