Return type

D

danieljmathew

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
 
S

Steffen

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
 
C

CBFalconer

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 */;
 
A

akarl

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
 
D

Default User

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
 
D

danieljmathew

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top