INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Casting an array to a record

Casting an array to a record

(OP)
I'm trying to do the following C code in Ada

CODE --> Code in C

char x[MAXX];
struct yyy { ... } *y;
...
y = (struct yyy*) x;
I'm writing a comms interface.  The problem I've got is that the data is coming in as a stream of bytes, which I store in an array.  After examining bytes 2 and 3, I know what type of record it is.  How do I now cast or copy this array to a record?

I tried using unchecked_conversion but it doesn't like variable sized arrays.
 

RE: Casting an array to a record

Hi xwb,

This should help you:

with Ada.Unchecked_Conversion;

procedure Test is

   --
   -- Define types to reprsent data coming in
   --
   type ByteType is range 0..2**8-1;
   for ByteType'Size use 8;
   
   type DataType is array (Natural range <>) of ByteType;
   pragma Pack (DataType );
   
   --
   -- Example of types that might be needed in the application
   --
   type ModeType is (On , Off , Test);
   for ModeType use (On   => 2#0000_0000# ,
                     Off  => 2#0001_0000# ,
                     Test => 2#0011_1000# );
   for ModeType'Size use 8;                   
   
   type HeightType is range 0..2**8-1 ;
   for HeightType'size use 8;

   --
   -- Define record type and lay it out using representation
   -- Could use a variant record if it is dependant on the data comming in.   
   --
   type RecordType is
   record
      Mode   : ModeType ;
      Height : HeightType ;
   end Record ;
   
   for RecordType use
   record
      Mode   at 0 range 0..7 ;
      Height at 0 range 8..15;
   end record ;

                            
   TheData   :  DataType := (0 => 0 ,  
                             1 => 22);
   TheRecord :  RecordType ;                                           
                                                
begin

              
   if TheData (1) = 22
   then
   
     ConstrainAndUseData:
     declare
        --
        --  Constrain the array type to the correct length
        --
        subtype ConstrainedType is DataType (TheData'Range);


        function Convert is new Ada.Unchecked_Conversion (Source => ConstrainedType,
                                                          Target => RecordType );
    
      begin                                                   
  
        --
        -- Convert to record format
        --
        TheRecord := Convert (S => TheData);

        if TheRecord.Mode'Valid  -- Ada95 only
        then
           --
           -- Safe to use Mode
           --
           null;
        end if;
        
      end ConstrainAndUseData;
   end if;

end Test;
 

Derek Russell
www.the-technical-diaries.com
www.objektum-solutions.com  

RE: Casting an array to a record

This is a fairly common problem, especially when dealing with unconstrained types.  

Instead of checking the individual bytes, you could define a variant record, provide representation and do the checking and conversion in 1 go.

If you'd like to chat further, let me know.

Regards,
Derek

Derek Russell
www.the-technical-diaries.com
www.objektum-solutions.com  

RE: Casting an array to a record

(OP)
Thanks - I can see what you're doing.  Once it has worked out what type it is, it is casting to the correct type.  The eureka line is

CODE

subtype ConstrainedType is DataType (TheData'Range);
I tried all sorts of combinations with unchecked_conversion but it just wouldn't compile.

RE: Casting an array to a record

(OP)
Compiler: gnat - basically gcc 4.1.2.
Code snippet: don't worry about it.  Your eureka line fixed it.

It is on one of those write only machines.  We can put stuff in but it is really difficult getting anything out.  Floppies and memory sticks don't work.  We have to burn DVDs and get them declassified.  Really not worth the effort.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close