Array with a structure

This is a discussion on Array with a structure within the C Programming forums, part of the General Programming Boards category; Here is my problem. I have to create a data structure to overlay an array of unsigned 8-bit values with ...

  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,211
    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,682
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,454
    Code:
    union test
    {
       unsigned char u[8];
       float f;
    };
    Like that?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,454
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,211
    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, 12:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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