Copy struct into char array

前端 未结 6 2009
面向向阳花
面向向阳花 2020-12-31 15:41

I am learning C and have a question about structs.

I have a

struct myStruct {
    char member1[16];
    char member2[10];
    char member3[4];
};         


        
相关标签:
6条回答
  • 2020-12-31 15:52

    you could do the following if you have a myStruct variable named st:

    strcpy(foo, st.member1);
    strcat(foo, st.member2);
    strcat(foo, st.member3);
    
    0 讨论(0)
  • 2020-12-31 15:57

    You can't just directly copy the whole thing, because the compiler may arbitrarily decide how to pad/pack this structure. You'll need three memcpy calls:

    struct myStruct s;
    // initialize s
    memcpy(foo,                                       s.member1, sizeof s.member1);
    memcpy(foo + sizeof s.member1,                    s.member2, sizeof s.member2);
    memcpy(foo + sizeof s.member1 + sizeof s.member2, s.member3, sizeof s.member3);
    
    0 讨论(0)
  • 2020-12-31 16:01

    Yes, it is possible.

    There are different ways you can go about doing this. Below are the two simplest methods.

    struct myStruct  myVar;
    
    /* Initialize myVar */
    ...
    
    memcpy (foo, &myVar, sizeof (myStruct));
    

    Or if you are dealing with a pointer ...

    struct myStruct *  myVarPtr;
    
    /* Initialize myVarPtr */
    ...
    
    memcpy (foo, myVarPtr, sizeof (myStruct));
    

    Note that when copying a structure to/from a character array like this, you have to be very careful as structure sizes are not always what you might first think. In your particular case, there might not be any issues; but in general, you should at least be aware of potential padding, alignment and type size issues that may change the size of your structure.

    Hope this helps.

    0 讨论(0)
  • According to the C Standard (6.2.6 Representations of types)

    4 Values stored in non-bit-field objects of any other object type consist of n × CHAR_BIT bits, where n is the size of an object of that type, in bytes. The value may be copied into an object of type unsigned char [n] (e.g., by memcpy); the resulting set of bytes is called the object representation of the value.

    So you can write simply

    unsigned char foo[sizeof( struct myStruct )];
    struct myStruct s = { /*...*/ };
    
    memcpy( foo, &s, sizeof( struct myStruct ) );
    

    Take into account that you could copy the data members separatly in one array. For example

    unsigned char foo[30];
    struct myStruct s = { /*...*/ };
    
    unsigned char *p = foo;
    memcpy( p, s.member1, sizeof( s.member1 ) );
    memcpy( p += sizeof( s.member1 ), s.member2, sizeof( s.member2 ) );
    memcpy( p += sizeof( s.member2 ), s.member3, sizeof( s.member3 ) );
    
    0 讨论(0)
  • 2020-12-31 16:08

    The size of struct myStruct is sizeof(struct myStruct) and nothing else. It'll be at least 30, but it could be any larger value.

    You can do this:

    char foo[sizeof(struct myStruct)];
    
    struct myStruct x; /* populate */
    
    memcpy(foo, &x, sizeof x);
    
    0 讨论(0)
  • 2020-12-31 16:10

    Pretty simple with memcpy.

    char foo[30];
    struct myStruct s;
    
    s.member1 = //some data
    s.member2 = //some data
    s.member3 = //some data
    
    memcpy(foo, &s, 30);
    
    0 讨论(0)
提交回复
热议问题