Best way to copy data of one structure to the other

This is a discussion on Best way to copy data of one structure to the other within the C Programming forums, part of the General Programming Boards category; Assuming i have a pointer to a structure StructA a* which contains value. Now in between computation i want to ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    15

    Best way to copy data of one structure to the other

    Assuming i have a pointer to a structure

    StructA a* which contains value. Now in between computation i want to keep a backup of the structure by storing the a in b which is also an object of StructA i,e StructA b*.
    What is the best way to copy the structure .
    i cant to b=a coz that wud point reference b to a and not a backup.
    I tried memcopy(b,a,sizeof(a)) but i realised sizeof(a) gives the size of the structure and not the contents

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    979
    Code:
    *b = *a;

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Just do
    structA b = *a;

    Assuming your struct does NOT contain any pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    15
    Quote Originally Posted by Salem View Post
    Just do
    structA b = *a;

    Assuming your struct does NOT contain any pointers.
    Unfortunately it does .. i am using libpq module .

    using
    structA b = *a; (in my case PGresult op=*res
    results in
    pgdb.c:76:5: error: variable Ďopí has initializer but incomplete type
    pgdb.c:76:18: error: dereferencing pointer to incomplete type
    pgdb.c:76:15: error: storage size of Ďopí isnít known
    which is understandable

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Then you're stuck.

    If the type is incomplete, there is no way you can make "a backup copy", since you can't find out what size it is, or what it contains.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Those errors mean that you are missing actual definition of the struct at that point, which likely means you are missing some #include for it.
    If the struct itself contains pointer then you're best to write a "copy" method for the struct and use that.
    Internally within the copy method I would start with a *this = *that; (i.e. one line complete copy) and then just correct the members that are pointers.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by aboosoyeed View Post
    Unfortunately it does .. i am using libpq module .
    Have you checked whether this module has an pre-built copy function for this struct?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fastest copy of structure elements
    By Chris_1980 in forum C Programming
    Replies: 9
    Last Post: 01-31-2012, 10:53 AM
  2. structure copy
    By vjefcoat in forum C Programming
    Replies: 7
    Last Post: 12-01-2010, 06:14 AM
  3. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  4. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  5. Copy structure elements into byte array
    By irncty99 in forum C Programming
    Replies: 4
    Last Post: 03-15-2005, 12:58 PM

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