🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2161

Re: returning multiple values in C

#2161
From: Graham Coles
Date: Mon, 04 Mar 1996 04:09
47 lines
1737 bytes
In article <4fbg7p$9u7@solutions.solon.com>, Herman Rubin
<hrubin@stat.purdue.edu> writes
>In article <4fa7gv$2te@solutions.solon.com>,
>HASDI RODZMANN HASHIM <hasdi@engin.umich.edu> wrote:
>>So far I have come across three ways of returning multiple values
>>from a C function. 

[snip]

>>I don't really have that much experience in C, so I would appreaciate
>>some pointers on which approach to use as well as their pros and cons.
>
>The problem is not unique to C; I know of NO language which does even
>a fair job of this.

Not even:

   (defun foo ()
      (list fooval1 fooval2))     ;)

To try and keep a 'C' tone on this, if you really must return 2 values,
you could always try:

int *Cfoo()
{
        static int rval[2];

        rval[0] = fooval1;
        rval[1] = fooval2;

        return rval;
}

Then call with

int *returned;

returned = Cfoo();

Add a suitable number of const keywords to taste ...

The downside is the cost in static storage, however, so don't use it
frequently for large numbers of return values.

--
Graham Coles           |      e-mail: graham@scitec.demon.co.uk
Sci-Tec Software       |      flames: bill@microsoft.com     :)
Technical Details
Message-ID: <4hdqej$8mt@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4fa7gv$2te@solutions.solon.com> <4fbg7p$9u7@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: Graham Coles <graham@scitec.demon.co.uk>
Newsgroups: comp.lang.c.moderated
Subject: Re: returning multiple values in C
Date: 3 Mar 1996 22:09:55 -0600
Organization: Sci-Tec Software
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4hdqej$8mt@solutions.solon.com>
References: <4fa7gv$2te@solutions.solon.com> <4fbg7p$9u7@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: Turnpike Version 1.10 <z5W0tcVhqGfVfh2lSJpSSlkSb1>