placing struct members into an array

This is a discussion on placing struct members into an array within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to place the members of a struct sequentially into an array so that I can send ...

  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
    21,450
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 Struct to Uint8_t array help please?

    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, 05: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

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