Returning several values (was Re: The D programming language)

GAY%CLSEPF51.BITNET at CUNYVM.CUNY.EDU GAY%CLSEPF51.BITNET at CUNYVM.CUNY.EDU
Thu Mar 10 07:18:57 AEST 1988


To return several values from a function: what's wrong with using

struct foo {
  int a, b;
};

struct foo fun(a, b)
int a, b;
{
    struct foo temp;

    temp.a = a / b; temp.b = a % b;

    return(temp);
}

I know this isn't as elegant as the proposed method (you have to extract
the values from the struct, declare a special purpose structure ...), but
it works and exists in at least one implementation of C (Lattice C). I
don't know if this is in the ANSI standard, if it isn't it is a fairly
logical extension (not needing any new syntax such as the square brackets
proposed), and it can even qualify as having prior use ! (I have a vague
memory of seeing this feature in other C compilers, but as I've never used
many I'm not sure if it is widespread or not (In other words, I hope I'm
not saying something that everybody knows ...)).

On another subject, the 9876543210L constant, the Lattice C compiler
accepts it without flinching. However, such a constant has a real value of
2147483647 (0x7fffffff) (as shown by a printf) which shows that the
compiler detected the overflow condition, but didn't signal it !
(Otherwise, the value would have probably been 1286608618 ...).

David Gay                                   GAY at CLSEPF51.bitnet

I disclaim all responsability for the absence of a disclaimer.



More information about the Comp.lang.c mailing list