Thread: copy struct contents

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    27

    copy struct contents

    hi,
    How can I write a function to copy the contents of struct first to second ?
    Code:
    typedef struct copyone{ char key[32];char value[32];}copyone,copytwo;
    typedef struct first{ char data[83];copyone *ptrone; } first;
    
    typedef struct second{ char name[83];copytwo *ptrtwo; } second;

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Eh? Why are you typedefing the same type twice? (copyone and copytwo). and then defining the same type twice (first, second).

    What have you tried so far?

    --
    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.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    hi,
    So far I am doing this to copy the contents(which is working just fine), each time I need the values of the "first" renewed.

    Code:
    n = malloc(sizeof(second));
    p = malloc(sizeof(first));
     for(i=0; i < count ; i++) {
            strcpy(n,p);
            strcpy(n->other[i].key,p->main[i].key);
            strcpy(n->other[i].value,p->main[i].value);
         }
    But I want to put the above code in a function that accepts any struct and copies the contents into "first" struct .

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The use of strcpy() does not look correct here. However, I think that you are making some more fundamental mistake.

    Instead of using meta-syntactic names like first and second, give the real names that you are using in the program, and tell us what you are trying to do. (As in the goal of this part of the program, not the implementation detail like "copy the contents of struct first to second".)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    Well...
    I meant to say copy the contents of the second struct to the first.
    There was a hurried typo.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bandal27
    I meant to say copy the contents of the second struct to the first.
    Well, it really depends. Consider this example program:
    Code:
    #include <stdio.h>
    
    typedef struct MappedType
    {
        char key[32];
        char value[32];
    } MappedType;
    
    int main()
    {
        MappedType x = {"ball", "red"};
        MappedType y;
        y = x; /* Copy the contents of x to y. */
        printf("%s %s\n", y.key, y.value);
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    this isn't in a funtion

    This isn't in a function - but the result is that the contents of one struct are copied into a second.

    http://cboard.cprogramming.com/showthread.php?t=110748

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cus
    This isn't in a function - but the result is that the contents of one struct are copied into a second.
    That would be using an assignment operator, in particular, copy assignment (also known as simple assignment). I demonstrated it in my previous post.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    Quote Originally Posted by laserlight View Post
    Well, it really depends. Consider this example program:
    Code:
    #include <stdio.h>
    
    typedef struct MappedType
    {
        char key[32];
        char value[32];
    } MappedType;
    
    int main()
    {
        MappedType x = {"ball", "red"};
        MappedType y;
        y = x; /* Copy the contents of x to y. */
        printf("%s %s\n", y.key, y.value);
        return 0;
    }
    Laserlight,
    Can this be used for my kind of structs too...?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bandal27
    Can this be used for my kind of structs too...?
    What exactly are your kind of structs? If you are talking about a struct that has a pointer to another struct or an element of an array that should be deep copied, then no, simple assignment is not enough since it would assign only the address stored in the pointer without assigning the contents pointed to. For that you may wish to use memcpy().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. splitting file contents into struct
    By explosive in forum C++ Programming
    Replies: 7
    Last Post: 05-07-2004, 04:50 AM