🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: fj.lang.pascal
Article #203

Re: Pascalのunion

#203
From: Tomohiko Sakamoto
Date: Sun, 16 Nov 1997 00:00
70 lines
2288 bytes
In article <346B423A.1ED1@be.mbn.or.jp>,
	mekemeke <mekemeke@be.mbn.or.jp> writes:
> $BA*BrDj?t$H$O>e$NNc$G$O!"(B
>  red green blue
> $B$KAjEv$9$k$H;W$&$N$G$9$,!";XDj$9$k$H$-$K$O$=$N$h$&$J;XDj$O$7$^$;$s$h$M!)(B
> $BNc$($P!"(B
>  sample.word = 0;
> $B$N$h$&$K!#(B
> $B#C$J$I$G$O!"A*BrDj?t$H$$$&35G0$9$iB8:_$7$J$$$N$G$9$,!"(B
> $B$3$l$O!"$I$N$h$&$J;~$K;H$&$b$N$J$N$G$7$g$&$+!)(B

union $B$N$I$N%a%s%P!<$r;2>H$9$k$N$+$r<B9T;~$KCN$k$?$a$K$O!"(B
$B!VA*BrDj?t!W$KAjEv$9$k$b$N$,(B C $B$G$bI,MW$G$7$g$&!#(B

#include <stdio.h>

enum color { red, grean, blue };

struct s {
    enum color tag;
    union { char c; int i; double d; } u;
};

void show(struct s *p)
{
    switch (p->tag) {
    case red : printf("c = '%c'\n", p->u.c); break;
    case grean : printf("i = %d\n", p->u.i); break;
    case blue :  printf("d = %g\n", p->u.d); break;
    }
}

main()
{
    struct s s;

    s.tag = red;   s.u.c = 'z'; show(&s);
    s.tag = grean; s.u.i = 123; show(&s);
    s.tag = blue;  s.u.d = 3.14; show(&s);
}

======================================================================
program main(output);
    type color = (red, grean, blue);
        union = record
                    case tag : color of
                    red :   (c : char);
                    grean : (i : integer);
                    blue :  (r : real)
                end;
    var u : union;

    procedure show(var u : union);
    begin
        case u.tag of
            red :   writeln('c = ', u.c);
            grean : writeln('i = ', u.i);
            blue :  writeln('r = ', u.r)
        end
    end;

begin
    u.tag := red;   u.c := 'z'; show(u);
    u.tag := grean; u.i := 123; show(u);
    u.tag := blue;  u.r := 3.14; show(u)
end.

--
$B:dK\CRI'(B	sakamoto@sm.sony.co.jp
Technical Details
Message-ID: <64mcdh$jfp@csdnews.sm.sony.co.jp>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <346B423A.1ED1@be.mbn.or.jp>

Show Full Headers
Complete Article Headers
X-Google-Language: Japanese,JIS
X-Google-Thread: ff90a,41ce56e12aa187d5
X-Google-Attributes: gidff90a,public
From: sakamoto@sm.sony.co.jp (Tomohiko Sakamoto)
Subject: Re: Pascalのunion
Date: 1997/11/16
Message-ID: <64mcdh$jfp@csdnews.sm.sony.co.jp>
X-Deja-AN: 289916181
References: <346B423A.1ED1@be.mbn.or.jp>
Organization: ITC, Sony Corporation
Reply-To: sakamoto@sm.sony.co.jp
Newsgroups: fj.lang.pascal