Thread: Struct to Uint8_t array help please?

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    Question Struct to Uint8_t array help please?

    Hi

    I have a struct variable that contains some data:

    Code:
    typedef struct {
            uint8_t commandIssued;
    	uint16_t oraginAddress;
    	bool success;
    } PACK AppCommandAck_t;
    
    AppCommandAck_t bob;
    I wish to place this variable and its contents into a uint8_t array:

    Code:
    Uint8_t bobHolder[100];
    I need something like:

    Code:
    bobHolder = bob;
    I do not want to copy each byte iteratively from the struct into the array. Does anyone know how I might achieve this operation?

    thanks

    D

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, but, what? Do you want the uint8_t, the uint16_t, and the bool in the array, or just the uint8? If you want them all, why an array of 100, since one struct should take 4 bytes. If you just want the uint8, why not copy each byte iteratively from the struct to the array? It will take two lines of code, three if you're the kind of person who always has to have braces on their for loops, and will run in approximately zero time, and besides I'm pretty sure there's literally no other way to do it.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quit often when you have a requirement "to have an array of <some type that represents a byte>" you are actually after transmitting it as a packet, storing it to a file, or some closely related subject.

    In general, you can actually achieve that by taking the address of the struct itself, and casting that to the relevant type, rather than producing an array from it.

    If that is not sufficient, then you need to use some function that does the above trick and copies the resulting pointer content into an array. There are library functions, such as memmove and memcpy that does this already.

    --
    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.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I do not want to copy each byte iteratively from the struct into the array. Does anyone know how I might achieve this operation?
    Unfortunately, that's the only way to guarantee success.

    > PACK AppCommandAck_t;
    Assuming for the moment that PACK (for your compiler) instructs the compiler to pack the structure, I don't think there's an absolute guarantee that the compiler will always do what you ask. Further, the syntax for asking for a packed structure varies from one compiler to another, so the result isn't really that portable.

    > uint16_t oraginAddress;
    If you're sending this to another machine, you need to take care of Endian issues.
    Assuming that PACK does what you want, and memcpy() does what you want as well, there is no short-cut to solving endian issues.
    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.

  5. #5
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    Sorry for jumping in on this thread, but I am trying to acheive something similar.

    Quote Originally Posted by matsp View Post
    In general, you can actually achieve that by taking the address of the struct itself, and casting that to the relevant type, rather than producing an array from it.
    This sounds exactly what I need but I don't really understand what this means. I assume its somethikng to do with pointers, but what sort of syntax would I expect to see?

    --dave

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "I do not want to copy each byte iteratively from the struct into the array."

    You mean all 7 or 8 of them! The way I understand it, this is exactly what the computer will have to do anyway, so probably the most effective and efficient way to program it will be to do it at the lowest feasible level which in this case seems to be THE BYTE.

    Maybe something crucial here is what's to be done with the array.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    memcpy() is generally optimized to use 32 or 64-bit copies when at all possible (this often assumes certain alignment and such, so it may not always be possible).

    --
    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
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    this is my current solution for this:

    Code:
    typedef struct 
    {
        char descriptor_msg[5];                
        vu16 address;                       
        vu8 space01;
        char cmdmsg01[22];                    
        vu8 CR;
        vu8 LF;
    } Send_status_off;
    
    Send_status_off MsgSend_status_off;
    
    // declarations of struct members
    
    union status_on_union
        {
             char status_msg_off[32];
              struct MsgSend_status_on;
        } status_on_union;
        
        union status_off_union
    I'm assuming that I will end up with an array called status_msg_off with each member from the struct contained within it in a sequential order (at least that's what I am trying to acheive)

    Will this do that? (I am not able to test as yet, since there are several other issues in the rest of the code to fix before it will compile)

    --dave

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, "struct MsgSend_status_on" is in no way a valid thing -- either you think MsgSend_status_on is a valid type (hint: it isn't), in which case you have no name for the variable, or you think you are declaring a variable of type "struct". Presumably you mean something like
    Code:
    union status_on_union
    {
        char status_msg_off[32];
        Send_status_on MsgSend_on;
    }
    This also assumes there is no padding in your struct, which I am doubtful of.

  10. #10
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    yes, that is what I meant, completely misread which was the type and which was the variable name.

    Thanks

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM