How to copy one struct in C to another?

This is a discussion on How to copy one struct in C to another? within the C Programming forums, part of the General Programming Boards category; hi all, with the help from people in this forum i achieved this process when concerning arrays: as seen here: ...

  1. #1
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    How to copy one struct in C to another?

    hi all, with the help from people in this forum i achieved this process when concerning arrays: as seen here: How do you copy one array in C to another?
    I would like to achieve the same effect with structure's now but am receiving some errors.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct one
    {
    int x;
    int y;
    } point;
    
    point top = {2,15};
    
    struct two 
    { 
    int a;
    int b;
    }blunt;
    
    int main ()
    {
    memcpy(two, one, sizeof one);
    return 0;
    }
    The errors are as follows:
    error: ‘two’ undeclared (first use in this function)
    error: (Each undeclared identifier is reported only once
    error: for each function it appears in.)
    error: ‘one’ undeclared (first use in this function.

    I'm sure I am not referencing the source structure and destination structure, for memcpy () correctly, also is sizeof 'one' correct? Does the second structure have to be a typedef?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So struct one and struct two are types, just like int and float and char. You need to copy into actual variables. You have one variable of type struct one ("struct one" is the same type as "point", due to the typedef), called top. You have a variable blunt of type struct two.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    You also need to pass the address of those variables to memcpy, because it expects pointers.
    Arrays automatically decays to pointers to their first element when passed as an argument, so you got away with not doing that on those.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    does that satisfy the question though? how to copy one structure into another in C? wouldn't that be copying variables into another?

    i'm not saying your wrong btw, I dont know the answer to that myself to be honest.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by cus View Post
    does that satisfy the question though? how to copy one structure into another in C? wouldn't that be copying variables into another?
    Yes it would. In computer memory, it's all bits and data anyway, so memcpy takes that data and puts it into the other struct.
    The type of the variables determine how the compiler - and ultimately, your code - treats that data.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by cus View Post
    does that satisfy the question though? how to copy one structure into another in C? wouldn't that be copying variables into another?

    i'm not saying your wrong btw, I dont know the answer to that myself to be honest.
    What do you think the difference is? The variables are structures.

  7. #7
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    yes haha, offcourse they are, thanks for that I'l have a try in a moment

  8. #8
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    ok i tried these two in memcpy(), and wasn't sure what 'sizeof' should be and the errors i'm now getting seem to re-enforce that:

    Code:
    memcpy(&blunt, &point, sizeof one);
    memcpy(&blunt, &point, sizeof point);
    The errors are:
    error: expected expression before ‘point’
    error: too few arguments to function ‘memcpy’

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I guess point and one are structure types. If so you have to wrap them with ()'s, i.e. sizeof(one), sizeof(point), sizeof(int), etc. However, int num; sizeof num is fine.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by cus View Post
    ok i tried these two in memcpy(), and wasn't sure what 'sizeof' should be and the errors i'm now getting seem to re-enforce that:

    Code:
    memcpy(&blunt, &point, sizeof one);


    The errors are:
    error: expected expression before ‘point’
    error: too few arguments to function ‘memcpy’
    Once again, point is a type, like int or float or char. You must memcpy into the address of a variable. The only variable you have of that type is "top".

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    That is why you should always do sizeof(something), not sizeof something as some like to do!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    thanks all

    thanks tabstop, twomers and elysia.

    I'm presuming this works now, i got your hint tabstop.
    I changed the second structure to a typedef, referenced 'top' in memcpy() correctly and created another variable for the second structure so i could read the values into it. Thanks again. btw is this ok?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct one
    {
    int x;
    int y;
    } point;
    
    point top = {2,15};
    
    typedef struct two 
    { 
    int a;
    int b;
    } blunt;
    
    blunt bottom;
    
    int main ()
    {
    memcpy(&bottom, &top, sizeof top);
    printf("The second structure values are %d and %d\n", bottom.a, bottom.b);
    
    return 0;
    
    }
    Also is there a way to show my problem has been solved i.e. a forum feature - or even a thank system?

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Looks OK. Does it work? I recommend adopting Elysia's approach and always use sizeof().

  14. #14
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    Quote Originally Posted by twomers View Post
    Looks OK. Does it work? I recommend adopting Elysia's approach and always use sizeof().
    yes it works eliysia recommended this method in a previous post to avoid errors, i just forgot to put it in, i shall use it always from now on. By the way how long do post's on this forum stay active for? Will they still be here for example if I searched in a couple of months time?

  15. #15
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    There are posts going back to 2001 so a long time. A Brief History of Cprogramming.com contains the archive. The posts in the other forums are the more active ones.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. copy struct to class
    By guda in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2003, 09:27 PM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM

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