Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: returning more than one value |
| Date | 2011-05-28 15:52 -0700 |
| Organization | None to speak of |
| Message-ID | <ln4o4etp3p.fsf@nuthaus.mib.org> (permalink) |
| References | <4de14540$0$1930$bbae4d71@news.suddenlink.net> <poadnfx_sOmI03zQnZ2dnUVZ_jSdnZ2d@giganews.com> |
Joe Wright <joewwright@comcast.net> writes:
> On 5/28/2011 14:55, Bill Cunningham wrote:
>> I came across a tutorial the other day that said a function can return
>> more than one value. How is this done? Pointers. Can someone please show me
>> an example. Say I want to return a double or an int.
>
> A function has a type and may (or not) return a value. Functions do not
> return multiple values.
But functions can return values that *contain* multiple values,
particularly by returning a struct value (see examples elsethread).
On the other hand, this technique isn't commonly used unless the
multiple values are logically part of a single entity. It requires the
caller to create an object of the struct type to hold the result, then
extract the individual values from that object. It's often more
convenient for the caller to pass pointers to individual variables.
Here's a program I just posted (based on Stefan Ram's program) that uses
the struct technique:
#include <stdio.h>
struct s {
int i;
double d;
};
struct s f( void )
{
struct s result;
result.i = 11;
result.d = 12.25;
return result;
}
int main( void )
{
const struct s returned_value = f();
printf( "%d %g\n", returned_value.i, returned_value.d );
return 0;
}
And here's a version that doesn't use the struct:
#include <stdio.h>
void f( int *pi, double *pd )
{
*pi = 11;
*pd = 12.25;
}
int main( void )
{
int i;
double d;
f(&i, &d);
printf( "%d %g\n", i, d );
return 0;
}
As you can see, a lot of the extra code to pack the information into a
struct and then unpack it is no longer necessary.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
returning more than one value "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 14:55 -0400
Re: returning more than one value Keith Thompson <kst-u@mib.org> - 2011-05-28 12:17 -0700
Re: returning more than one value "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 15:26 -0400
Re: returning more than one value "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 15:31 -0400
Re: returning more than one value Joe Wright <joewwright@comcast.net> - 2011-05-28 15:33 -0400
Re: returning more than one value Keith Thompson <kst-u@mib.org> - 2011-05-28 15:52 -0700
Re: returning more than one value Kenneth Brody <kenbrody@spamcop.net> - 2011-05-31 12:15 -0400
Re: returning more than one value "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 15:34 -0400
Re: returning more than one value Keith Thompson <kst-u@mib.org> - 2011-05-28 15:45 -0700
Re: returning more than one value Shao Miller <sha0.miller@gmail.com> - 2011-05-28 15:09 -0500
Re: returning more than one value "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 16:14 -0400
Re: returning more than one value "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-05-28 16:32 -0400
csiph-web