Thread: placing struct members into an array

  1. #1
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83

    placing struct members into an array

    Hi,

    I am trying to place the members of a struct sequentially into an array so that I can send one byte at a time down a serial line, but I can see no way of pulling each individual member out to fill the array.

    Code:
    int array[10];
    int i;
    
    struct fillArray 
    {    
        member1
        member2
        member3
    } arrayFill;
    
    for(i=0;i<=10;i++)
    {
        //how do I get the data from the struct to fill this loop?
        array[i]
    }
    It has been suggested that I use a union type, but I don't know anything about this as I have never come across it before, and the web was not too helpful on this occasion.

    Thanks
    Dave

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One option would be to use memcpy() into an array of char, but what is the exact definition of arrayFill? If it has pointer members then a simple memcpy() would probably not be the correct approach.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    Its an array of chars and ints, no pointers involved, I will have a look at memcpy(), thanks

    --dave

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to look at my comments in http://cboard.cprogramming.com/showthread.php?t=111125

    where almost the same question is asked.

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

  5. #5
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    thanks for the link, I'll have a look at that

    --dave

  6. #6
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    I have this code to transfer the contents of my struct into a char array:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct msg_on_send
    {
       char descriptor_msg[5];
       int  address;
       char space;
       char cmdmsg[5];
       char CR;
       char LF;
    };
    
    size_t serialize(unsigned char *dst, const struct msg_on_send *object)
    {
       size_t i = 0;
    
       memcpy(&dst[i], &object->descriptor_msg, sizeof object->descriptor_msg);
       i += sizeof object->descriptor_msg;
    
       memcpy(&dst[i], &object->address, sizeof object->address);
       i += sizeof object->address;
    
       memcpy(&dst[i], &object->space, sizeof object->space);
       i += sizeof object->space;
    
       memcpy(&dst[i], &object->cmdmsg, sizeof object->cmdmsg);
       i += sizeof object->cmdmsg;
    
       return i;
    }
    
    void showbytes(const void *object, size_t size)
    {
       const unsigned char *byte;
       for ( byte = object; size--; ++byte )
       {
          printf("%02X", *byte);
       }
       putchar('\n');
    }
    
    int main()
    {
       int member;
       struct msg_on_send SendMsg_on[sizeof member] =
       {
             SendMsg_on.descriptor_msg = "$str_";
             SendMsg_on.address = 0x02;
             SendMsg_on.space = 0x20;
             SendMsg_on.cmdmsg = "op_en";
             SendMsg_on.CR = 0x0D;
             SendMsg_on.LF = 0x0A;
       };
       unsigned char buffer [ sizeof SendMsg_on[0] ];
       showbytes(buffer, serialize(buffer, by));
       
       system("PAUSE");    
       return 0;
    }
    In the original code this took a fixed array, is my modification of loading the struct members within main valid, if not, can you suggest what I could do instead?

    --dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional struct array help
    By Mostly Harmless in forum C++ Programming
    Replies: 2
    Last Post: 08-22-2006, 02:26 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM