Thread: Unions...

  1. #1
    Registered User
    Join Date
    Jul 2021
    Posts
    20

    Unions...

    Hi.

    I have been reading about unions.
    I have but together what i think is the solutions to the following:

    I have a float value and i would like to easily break it into 4 bytes and view each byte. Pretty straight forward!!

    So outside of main() i have this.

    Code:
    union 
      {
        float AsFloat;
        char AsByte;
      } FloatUnion;
    Inside main() i have this.

    Code:
    char float_union_bytes[4];
        
        FloatUnion.AsFloat = 24.65;
        float_union_bytes[0] = FloatUnion.AsByte[0];
        float_union_bytes[1] = FloatUnion.AsByte[1];
        float_union_bytes[2] = FloatUnion.AsByte[2];
        float_union_bytes[3] = FloatUnion.AsByte[3];
      
        VT100_Print_Hex(float_union_bytes[0], 42, 40, F_BLUE, B_BLACK, 1, 0);
        VT100_Print_Hex(float_union_bytes[1], 43, 40, F_BLUE, B_BLACK, 1, 0);
        VT100_Print_Hex(float_union_bytes[2], 44, 40, F_BLUE, B_BLACK, 1, 0);
        VT100_Print_Hex(float_union_bytes[3], 45, 40, F_BLUE, B_BLACK, 1, 0);
    The VT100_Print_hex() will print a value as hex on terminal, this is tested and working.

    So when i compile the above, i get this error.

    Code:
    main.c:60:45: error: subscripted value is neither array nor pointer nor vector
         float_union_bytes[0] = FloatUnion.AsByte[0];

    I dont know what its asking me for and to me, it looks correct.
    Can anyone help me with what i am missing, please.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try it with
    Code:
    union
      {
        float AsFloat;
        char AsByte[4];
      } FloatUnion;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2021
    Posts
    20
    Quote Originally Posted by Salem View Post
    Try it with
    Code:
    union
      {
        float AsFloat;
        char AsByte[4];
      } FloatUnion;
    This is identical to what I have already...

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Quote Originally Posted by Kisen View Post
    This is identical to what I have already...
    If that's true then why did you show something different?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Jul 2021
    Posts
    20
    Quote Originally Posted by john.c View Post
    If that's true then why did you show something different?
    Ahhhh. My mistake. I see whats different now. I am missing [4]. Its been a long day

    I will try this tomorrow when I'm on the PC and let you know if it worked.

  6. #6
    Registered User
    Join Date
    Jul 2021
    Posts
    20
    Quote Originally Posted by Salem View Post
    Try it with
    Code:
    union
      {
        float AsFloat;
        char AsByte[4];
      } FloatUnion;
    This worked. Thank you very much

  7. #7
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by Salem View Post
    Try it with
    Code:
    union
      {
        float AsFloat;
        char AsByte[4];
      } FloatUnion;
    Personally I prefer to use sizeof(float) rather than using 4 directly, then instead of directly referencing the bytes I would use a loop to access them, just gives that piece of mind that all bytes are covered in the unlikely event a float is not 4 bytes, would be a strange setup but it's not impossible, I've read of a setup where all integers besides char were 8 bytes long so it's not out of the question

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by awsdert View Post
    Personally I prefer to use sizeof(float) rather than using 4 directly, then instead of directly referencing the bytes I would use a loop to access them, just gives that piece of mind that all bytes are covered in the unlikely event a float is not 4 bytes, would be a strange setup but it's not impossible, I've read of a setup where all integers besides char were 8 bytes long so it's not out of the question
    Indeed. Some systems might have only one floating-point type so that float is the same as double. Some systems might even have 16- or 32-bit char, which makes sizeof (float) equal 2 or 1. Using sizeof is the safest way to go.

  9. #9
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    By the way if you want to study how the binary system of FPNs work I suggest taking a look at the core functions of my ALU project (I'm planning to clean it up later when I've got time for it, currently have to spend most of my time on job-hunting stuff), there are edge case bugs that I haven't resolved yet but it's enough to get you most of the way (the link is in my signature btw), if you happen to figure out a fix for those edge cases please do let me know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unions
    By jduke44 in forum C Programming
    Replies: 2
    Last Post: 10-26-2005, 03:50 PM
  2. Unions?
    By Sure in forum C Programming
    Replies: 8
    Last Post: 06-30-2005, 02:47 AM
  3. unions
    By mickle in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 11:46 PM
  4. Unions in c++
    By The Gweech in forum C++ Programming
    Replies: 5
    Last Post: 08-06-2002, 08:14 AM
  5. Unions?
    By hostensteffa in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2002, 06:01 AM

Tags for this Thread