473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Nov 15 '05 #1
5 1297
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

Nov 15 '05 #2
"da***********@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
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 15 '05 #3
da***********@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
Nov 15 '05 #4
da***********@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).

What about -1?

Brian
Nov 15 '05 #5
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.

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
6
by: Bruce W.1 | last post by:
The intent of my web service is an RSS feed from a blog. Originally I used a StringBuilder to make the XML and returned a string from the webmethod. But this doesn't display properly in IE. So...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
59
by: Michael C | last post by:
eg void DoIt() { int i = FromString("1"); double d = FromString("1.1"); } int FromString(string SomeValue) {
3
by: C++ | last post by:
According to Thinking in C++ "You cannot modify the return type of a virtual function during overriding.but there is a special case in which you can slightly modify the return type. If you¡¯re...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
9
by: Alexander Widera | last post by:
hi, is it possible to return an object of an unknown (but not really unknown) type with an method? i have the following situation: - a variable (A) of the type "object" which contains the...
9
by: hufaunder | last post by:
I have a class "TestSuper" that implements the interface "TestBase". The interface has a property of type "ReturnType". The class "TestSuper" does not return "ReturnType" but a derivation...
14
by: =?Utf-8?B?QmVu?= | last post by:
Hi all, I'm trying to understand the concept of returning functions from the enclosing functions. This idea is new to me and I don't understand when and why I would need to use it. Can someone...
4
by: fabian.lim | last post by:
Hi All, Im a newbie to C++, I am trying to customize the vector template STL to a template Class. My code is shown below. Im confused about something and maybe somebody here might be able to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.