Board index » delphi » C Union - Pascal Equivalent ?

C Union - Pascal Equivalent ?

In article <42pb4n$...@grovel.iafrica.com>,
burro...@telkom09.telkom.co.za says...

Quote

>Hi there.

>I am trying to convert C source to pascal.
>However, my C knowledge is limited.
>As far as I am led to beleive, a union
>constitutes many possible items, but only
>one of those items will be retured in the
>structure.
>The space allocated for the union matches
>the equivalent space for the largest item
>in the union.
>Blah, blah, <snip>!

What I usually do is use ABSOLTE.
Say you have this in C:

Union{
   Int Num;
   Char Ch[2];

Quote
} Whatever;

You could say in Pascal:

type
        Ch2 : Array[0..1] of Char;

Whatever        : Record
                     Num : Integer;
                     Ch  : Ch2 absolute Num;
                  End;

Then you could refer to Whatever.Num or Whatever.Ch and it would change
the other, like union.  Hope this helps, bye.

--
,______________________________________________________________,
|o-------------------------------------o----------------------o|
|| "I love messing with data" -Calvin  |  - Justin Kramer -   ||
|| "Ugh! BLAM! AAAAHHH!!!" -DeathMatch | - g...@cornell.edu - ||
|o-------------------------------------o----------------------o|
'--------------------------------------------------------------'

 

Re:C Union - Pascal Equivalent ?


Quote
>My problem is, I am filling a structure
>(in pascal) that obtains information from
>a C application vie the network, so I need
>to know how to determine what item was passed
>in the union, and then place the relevant info
>in a pascal structure that emulates the union.

Correct me if I'm wrong, but this sounds like a variant record.  What
determines the format the data in the area is read from is the name that
you use in C.  Pascal also lets you set which is which.

This is an example from "C for Pascal Programmers" by T.D.Brown.

Pascal:
type geom_figure =
 record
  area:real;
  case shape: (circle, triangle, square, point) of
  circle: (r: real);
  triangle: (a, b, c: real);
  square: (l, w: real);
  point: ();
 end;

Equivalent in C:
typedef struct {
 float area;
 enum {CIRCLE, TRIANGLE, SQUARE, POINT} shape;
 union {
  struct {float r;} circle;
  struct {float a,b,c;} triangle;
  struct {float l,w;} point;
 } figure;

Quote
} geom_figure;

I don't know if this will help you any, but I hope it will.

  URBT...@prodigy.com  
_______________mOOm__________________________________
Early to rise, early to bed, makes a man healthy, but socially dead. -
Animaniacs, Wheel of Morality

Re:C Union - Pascal Equivalent ?


Hi there.

I am trying to convert C source to pascal.
However, my C knowledge is limited.
As far as I am led to beleive, a union
constitutes many possible items, but only
one of those items will be retured in the
structure.
The space allocated for the union matches
the equivalent space for the largest item
in the union.

My problem is, I am filling a structure
(in pascal) that obtains information from
a C application vie the network, so I need
to know how to determine what item was passed
in the union, and then place the relevant info
in a pascal structure that emulates the union.

Any and all help will be greatly appreciated.
Thanks in advance
  James Burroughs
  Telkom SA
  burro...@telkom09.telkom.co.za

Re:C Union - Pascal Equivalent ?


In article <42pb4n$...@grovel.iafrica.com>, James Burroughs <burro...@telkom09.telkom.co.za> says:

Quote

>Hi there.

>I am trying to convert C source to pascal.
>However, my C knowledge is limited.
>As far as I am led to beleive, a union
>constitutes many possible items, but only
>one of those items will be retured in the
>structure.
>The space allocated for the union matches
>the equivalent space for the largest item
>in the union.

>My problem is, I am filling a structure
>(in pascal) that obtains information from
>a C application vie the network, so I need
>to know how to determine what item was passed
>in the union, and then place the relevant info
>in a pascal structure that emulates the union.

>Any and all help will be greatly appreciated.
>Thanks in advance
>  James Burroughs
>  Telkom SA
>  burro...@telkom09.telkom.co.za

The equivilant in Pascal is a variable record.
In both C and Pascal both records (or structures) and variable records
(or unions) are data types that take a specific amount of memory. In the
case of variable records (and unions), space is allocated for the
largest possible circumstance. This being the case, use blockread (binary
reading) to read x amount of bytes into the variable record....

Re:C Union - Pascal Equivalent ?


Quote
Terry Gaudet (tgau...@cam.org) wrote:

: In article <42pb4n$...@grovel.iafrica.com>, James Burroughs <burro...@telkom09.telkom.co.za> says:
: >
: >Hi there.
: >
: >I am trying to convert C source to pascal.
: >However, my C knowledge is limited.
: >As far as I am led to beleive, a union
: >constitutes many possible items, but only
: >one of those items will be retured in the
: >structure.
: >The space allocated for the union matches
: >the equivalent space for the largest item
: >in the union.
: >
: >My problem is, I am filling a structure
: >(in pascal) that obtains information from
: >a C application vie the network, so I need
: >to know how to determine what item was passed
: >in the union, and then place the relevant info
: >in a pascal structure that emulates the union.
: >
: >Any and all help will be greatly appreciated.
: >Thanks in advance
: >  James Burroughs
: >  Telkom SA
: >  burro...@telkom09.telkom.co.za

: The equivilant in Pascal is a variable record.
: In both C and Pascal both records (or structures) and variable records
: (or unions) are data types that take a specific amount of memory. In the
: case of variable records (and unions), space is allocated for the
: largest possible circumstance. This being the case, use blockread (binary
: reading) to read x amount of bytes into the variable record....

Actually it is posible, but not all pascalcompilers accept it.

type
  DataRec = record
    case Integer of
     0: Number : LongInt;
     1: Ptr : Pointer;
     2: Int1, Int2 : Integer;
     3: B1, B2, B3, B4 : Byte;
    end;
  end;

With this record you can read each type you want. You even don't have to
have variables of the same size as far as i know, so

case Integer of
 0: Number : LongInt;
 1: B1 : Byte;
end;

would probably also work.

Hope this helps.

--
----------------------------------------------------------------------------
Sander Niemeijer  (wiskunde)  | There are three kinds of people :
S.V.Niemei...@TWI.TUDelft.NL  | those who can count and those who can't
----------------------------------------------------------------------------

Other Threads