Board index » delphi » Equivalent of C 'Union'

Equivalent of C 'Union'

In C it is possible to look at the same area of memory thru different
variable types thru the use of the 'union' keyword.  For example, I
could define a structure containing two long ints, another structure
containing eight characters, and then union them together so that I
could look at the data either as a number or a bunch of characters.

Is there a similar provision in Pascal or am I going to have to
declare several record types, instance one of them, then use pointers
for all the rest of the redefinitions I want?

Thank you.

 

Re:Equivalent of C 'Union'


Quote
wherb...@rice.edu (Wayne Herbert) wrote:
>In C it is possible to look at the same area of memory thru different
>variable types thru the use of the 'union' keyword.  For example, I
>could define a structure containing two long ints, another structure
>containing eight characters, and then union them together so that I
>could look at the data either as a number or a bunch of characters.

Try a variant record, such as :

abc = record
  case integer of
    0 : (li1 : longint ;
         li2 : longint) ;
    1 : (car : array[1..8] of char)
end ;

Re:Equivalent of C 'Union'


Robert,

Thank you.  Indeed the variant works as advertised.  May I add a
further complication?  I'm adapting a COBOL based soundex routine to
Delphi (and Pascal).  I need to be able to look at the name to be
soundexed as:

character by character (OK - easy)
first two characters
first three characters
.
.
first five characters ( as a single field).

In COBOL this would be easy and would allow a statement like

if firstfive = 'abcde'

After I got the variant process working, I realized strings are a no
no, so now I'm back to the question of how can I look at a string as a
bunch of smaller strings.  Obviously if necessary, I'll go to counters
and character arrays, but the string manipulation facilities of pascal
do look kewl.

Many thanks.

Re:Equivalent of C 'Union'


Quote
wherb...@rice.edu (Wayne Herbert) wrote:
>Robert,

>Thank you.  Indeed the variant works as advertised.  May I add a
>further complication?  I'm adapting a COBOL based soundex routine to
>Delphi (and Pascal).  I need to be able to look at the name to be
>soundexed as:

>character by character (OK - easy)
>first two characters
>first three characters
>.
>.
>first five characters ( as a single field).

>In COBOL this would be easy and would allow a statement like

>if firstfive = 'abcde'

>After I got the variant process working, I realized strings are a no
>no, so now I'm back to the question of how can I look at a string as a
>bunch of smaller strings.  Obviously if necessary, I'll go to counters
>and character arrays, but the string manipulation facilities of pascal
>do look kewl.

If this were c.l.p.b, I'd suggest using the copy function to
extract portions of the string.  Your FirstFive example becomes,
"If Copy(s,1,5) = 'abcde' Then ...", where "s" is the original
string.

A variant record with TP strings:

TYPE tFive = RECORD
       Case Integer of
         1: (S: String);
         2: (Len : Byte;
             Five: Array[1..5] of Char);
      End;

If TP a 1-based array of characters can be used in an expression
just like a string.  You must realize that "five" will return
5-characters regardless of the length of "s".  If the string
contains fewer than five characters then you will get garbage.
You could pad the string to insure sufficient characters, but I
suggest using Copy(s, 1, 5).

    ...red

Re:Equivalent of C 'Union'


In article <339dd4d2.172639...@news.rice.edu>
           wherb...@rice.edu "Wayne Herbert" writes:

Quote
> In C it is possible to look at the same area of memory thru different
> variable types thru the use of the 'union' keyword.  For example, I
> could define a structure containing two long ints, another structure
> containing eight characters, and then union them together so that I
> could look at the data either as a number or a bunch of characters.

> Is there a similar provision in Pascal or am I going to have to
> declare several record types, instance one of them, then use pointers
> for all the rest of the redefinitions I want?

> Thank you.

Type
 TMyRecord = record
              Case Integer of
               0: First,second : LongInt;
               1: Text:Array[1..8] of char;
             end;

or something like that

--
Daniel Silverstone (Usually ;)

Look at the farstar Homepages on:
 http://www.farstar.demon.co.uk/

Try out Daniel's TPascal Page & Delphi 2.0 bits at
 http://www.farstar.demon.co.uk/Pascal/index.htm

Mail me & give comments (I know it's sparse ;)
 mailto:WebMas...@farstar.demon.co.uk

Other Threads