开发者

using memcpy to copy a structure [duplicate]

开发者 https://www.devze.com 2023-02-09 05:25 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Copying one structure to another
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Copying one structure to another

       struct node
        {
           int n; 
           struct classifier keys[M-1];
           struct node *p[M];
        }*root=NULL;

i have created newnode which is of type node

        (*newnode)->keys[i]

i want to copy data to keys structure from structure clsf_ptr which is also of same type can i do it like this,i don't want to initialize eac开发者_C百科h member function

       memcpy((*newnode)->keys[i], clsf_ptr) 


For a start, that should probably be:

memcpy(&(newnode->keys[i]), &clsf_ptr, sizeof(struct classifier));

(assuming newnode is a pointer-to-node, and clsf_ptr is a classifier`).

Also, struct assignment is legal in C, so you could just do:

newnode->keys[i] = clsf_ptr;

Note that both of these approaches only do a shallow copy. So if struct classifier has pointers to memory, the pointers themselves will be copied, rather than creating new copies of the memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消