Thread: Array with a structure

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    1

    Red face Array with a structure

    Here is my problem. I have to create a data structure to overlay an array of unsigned 8-bit values with a floating point variable.

    I'm pretty sure I have to use a structure/union, but I don't know where to begin with this. Any pointers, guys?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Could you clarify the question?

    Are you trying to share the same memory with different variables? If so, you're on the right track.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    My previous was pointing to some other topic which is called as BitField which is not what u where asking. Sorry about never read your post properly. About the struct overlay.

    Well, what is your application of using this overlay, I would be normally used to overlay a register while programming embedded systems. I use it while programming my board. So what is the application of using that in your case.

    ssharish

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    union test
    {
       unsigned char u[8];
       float f;
    };
    Like that?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Too many chars for a 32-bit float.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MacGyver View Post
    Too many chars for a 32-bit float.
    Who said 2 members of the union should be of the same size?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    Who said 2 members of the union should be of the same size?
    They don't have to be, but if you want to overlay one float with the same number of unsigned char's, then it's kind of useful if the union's members are all the same size.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, that's what I meant. Thanks, matsp.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure or Array?
    By epi_jimenez in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 02:45 PM
  2. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM