How to define a variant record type in C

This is a discussion on How to define a variant record type in C within the C Programming forums, part of the General Programming Boards category; Dear Sir or Madam: I am looking for help to define a variant record type in C. I am trying ...

  1. #1
    Diepvien_007
    Guest

    How to define a variant record type in C

    Dear Sir or Madam:
    I am looking for help to define a variant record type in C. I am trying to map a Ada variant record data type into C data type, but I really dont know how to convert it into C. Here is the code
    in Ada
    Code:
    type POWER is (GAS, STEAM, DIESEL, NONE);
    
       type VEHICLE (Engine : POWER) is
          record
             Model_Year : INTEGER range 1888..1992;
             Wheels     : INTEGER range 2..18;
             case Engine is
                when GAS    => Cylinders   : INTEGER range 1..16;
                when STEAM  => Boiler_Size : INTEGER range 5..22;
                               Coal_Burner : BOOLEAN;
                when DIESEL => Fuel_Inject : BOOLEAN;
                when NONE   => Speeds      : INTEGER range 1..15;
             end case;
          end record;
    
       Ford    : VEHICLE(GAS);
       Truck   : VEHICLE(DIESEL);
       Schwinn : VEHICLE(NONE);
       Stanley : VEHICLE(STEAM);
    Any C code example or explaining how to define this kind of data structure in C is greatly appreciated.

    DiepVien_007

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use C++ and build a class. Or, use C and enums. You cannot enforce the behaviour you want in C easily. Yes it can be done. No, it's not easy. Use C++.
    [edit]
    Code:
    /* type POWER is (GAS, STEAM, DIESEL, NONE); */
    typedef enum { NONE, GAS, STEAM, DIESEL } POWER;
    
    /*
       type VEHICLE (Engine : POWER) is
          record
             Model_Year : INTEGER range 1888..1992;
             Wheels     : INTEGER range 2..18;
             case Engine is
                when GAS    => Cylinders   : INTEGER range 1..16;
                when STEAM  => Boiler_Size : INTEGER range 5..22;
                               Coal_Burner : BOOLEAN;
                when DIESEL => Fuel_Inject : BOOLEAN;
                when NONE   => Speeds      : INTEGER range 1..15;
             end case;
          end record;
    */
    typedef struct {
    	unsigned char Model_Year;
    	unsigned char Wheels;
    	POWER Type;
    	union
    	{
    		struct
    		{
    			unsigned char Cylinders;
    		} Gas;
    		struct
    		{
    			unsigned char Boiler_Size;
    			unsigned char Coal_Burner;
    		} Steam;
    		struct
    		{
    			unsigned char Fuel_Inject;
    		} Diesel;
    		struct
    		{
    			unsigned char Speeds;
    		}
    	} Engine;
    } VEHICLE;
    
    /*   Ford    : VEHICLE(GAS); */
    VEHICLE Ford;
    
    SetWheels( &Ford, 4 ); /* This would assign the value. */
    SetYear( &Ford, 1983 ); /* This would assign the value. */
    SetEngine( &Ford, GAS ); /* This would prompt for the correct values. */
    It is up to you to enforce the values correctly. You really want C++.
    [/edit]

    Quzah.
    Last edited by quzah; 03-12-2003 at 02:39 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    DiepVien_007
    Guest
    Thanks alot quazz.
    Yup! I think I better convert all Ada variant record types into
    C++ classes, such as class VEHICLE.....

    DiepVien_007

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  3. Help getting multiple keypresses in a DOS compiler
    By Nongan in forum Game Programming
    Replies: 2
    Last Post: 04-01-2005, 09:07 PM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  5. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21