-1

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 each member function

       memcpy((*newnode)->keys[i], clsf_ptr) 
Gunnar Bernstein
  • 6,074
  • 2
  • 45
  • 67
asir
  • 12,843
  • 8
  • 24
  • 18
  • Duplicates (same user): [copying one structure to another](http://stackoverflow.com/questions/4932012/copying-one-structure-to-another) and [Copying one structure to another](http://stackoverflow.com/questions/4931123/copying-one-structure-to-another) – Paul R Feb 09 '11 at 11:38
  • 1
    why do you waste our time by asking this 3 times, althoug you accepted the answers? – stacker Feb 09 '11 at 11:51
  • 2
    I think it's a duplicate of [4932012](http://stackoverflow.com/questions/4932012/copying-one-structure-to-another), but not [4931123](http://stackoverflow.com/questions/4931123/copying-one-structure-to-another). This is about copying an array from one struct to another. [4931123](http://stackoverflow.com/questions/4931123/copying-one-structure-to-another) is about copying a whole struct. 4932012 was wrongly closed as a duplicate (but asking the same question again only makes the situation worse.) – finnw Feb 09 '11 at 11:52
  • 1
    @stacker The FAQ says "be nice". asir did not in fact ask the same question 3 times, they are different questions. – Jim Balter Feb 09 '11 at 13:30

1 Answers1

3

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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Oli Charlesworth @ newnode is a pointer to node but clsf_ptr is not a pointer to classifier – asir Feb 09 '11 at 11:40
  • @asir: What is it, then? If it's anything else, it's not a very good variable name! – Oliver Charlesworth Feb 09 '11 at 11:42
  • @Oli Charlesworth : it is just a instance of classifier, in structure classifier with instance clsf_ptr i have initialized some data, i have to copy that data to (*newnode)->keys[i] location – asir Feb 09 '11 at 11:46
  • @asir: Ok, see the updates to my answer above! – Oliver Charlesworth Feb 09 '11 at 11:50
  • @ Oli Charlesworth : sorry for bugging you again,just stated coding in C a month ago, in other case i have instance of node which is not a pointer and even that of classifier, so is this syntax correct memcpy((newnode->keys[i]),&clsf_ptr, sizeof(struct classifier))) – asir Feb 09 '11 at 12:17
  • If `newnode` is a pointer, then you need to use `->keys[i]`, otherwise you need to use `.keys[i]`. Either way, you need to provide `memcpy` with an *address*, so you must use the `&` operator. So either `&(newnode->keys[i])` or `&(newnode.keys[i])`. – Oliver Charlesworth Feb 09 '11 at 12:22