unions / functions

This is a discussion on unions / functions within the C Programming forums, part of the General Programming Boards category; I am having problems. I am currently learning C and have never used unions before. I have created the following ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    66

    unions / functions

    I am having problems. I am currently learning C and have never used unions before. I have created the following union:

    struct I_R_Record {
    char Record_Type;
    char Customer_code[6];
    char Part_Number [7];
    char Quantity [5];
    };

    struct Deletion_Record {
    char Record_type;
    char Customer_Code[6];
    };

    struct Creation_Record {
    char Record_Type;
    char Customer_Code [6];
    char Customer_Name [21];
    char Customer_Address[61];
    float Customer_Balance;
    long Credit_Limit;
    };

    union record {
    struct I_R_Record I_R;
    struct Deletion_Record Del;
    struct Creation_Record cre;
    };

    But I dont know how to use it in a function. I have passed it to a function as follows:

    /* Declaration */
    void collect_record( FILE *file_ptr, union record *data );


    then in main

    collect_record( the_file, the_union );

    but I dont know how to use it from this function. What I want to do is determine its type i.e. Record_Type. and then from this read the data in the appropriate way. Please could somebody tell me where I am getting stuck. Thanx

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    Same as a struct basically

    Code:
    void collect_record( FILE *file_ptr, union record *data ) {
       if ( data->I_R.Record_Type == 'I' ) {
       }
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    Cheers. I have spent ages with that but I must of got confused Because I have been trying data->I_R->Record_type. Anyway I will know from now on so thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 05:46 PM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM

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