Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Return type (http://www.velocityreviews.com/forums/t439192-return-type.html)

 danieljmathew@gmail.com 08-25-2005 07:19 AM

Return type

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

 Steffen 08-25-2005 08:02 AM

Re: Return type

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

 CBFalconer 08-25-2005 08:27 AM

Re: Return type

"danieljmathew@gmail.com" 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

 akarl 08-25-2005 11:27 AM

Re: Return type

danieljmathew@gmail.com 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

 Default User 08-25-2005 03:45 PM

Re: Return type

danieljmathew@gmail.com 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).

Brian

 danieljmathew@gmail.com 08-25-2005 04:22 PM

Re: Return type

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.

 All times are GMT. The time now is 07:01 PM.