#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    16
    Rep Power
    0

    Structure assignment


    Is it good idea to assign struct as
    Struct1 = struct2;
    or copy content from one to another.

    Can we really copy content of struct2 to struct1 by just
    struct1=struct2;

    This is in C and not C++ with operator overload.
    Thanks
    BK
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Yes you can do that.

    But beware that all you're doing is
    memcpy( &s2, &s1, sizeof s2 );

    If your struct contains any pointers, especially to dynamic memory, then you've got the potential for a lot of interesting problems.
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    I always thought it was kind of tacky that C allowed copying of structs but not copying of arrays. (In fact, if you just wrap your arrays into structs, you magically gain the ability to assign to arrays with the assignment operator.) But yes, you really can copy a struct with the '=' operator, and as salem said, it is a literal byte-for-byte copy, which is often not appropriate. So whether it is a good idea or not depends completely on what the fields of your struct represent.
  6. #4
  7. No Profile Picture
    Brony & F/OSS Advocate
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    Anaheim, CA (USA)
    Posts
    6,653
    Rep Power
    2476
    Originally Posted by Lux Perpetua
    I always thought it was kind of tacky that C allowed copying of structs but not copying of arrays. (In fact, if you just wrap your arrays into structs, you magically gain the ability to assign to arrays with the assignment operator.)
    An array variable in C is nothing more than a const pointer to the base address (0th element) of a fixed-size block of sequential memory. Thus, assuming the size of the array is know, the contents can be copied using the memcpy standard library function:
    c Code:
    #include <stdlib.h>
    #include <stdio.h>
     
    /* ... */
     
    auto  int  intArraySrc[] = { 1, 20, 43, 64, 59, 67 };
    auto  int  intArrayDest[6];
     
    /* Assuming that intArraySrc is in the current block scope, so
    its size is known by the compiler... */
    memcpy(intArrayDest, intArraySrc, sizeof(intArraySrc) );
     
    auto  int  looper;
    for (looper = 0; looper = 6; looper += 1)
    {
        printf("intArrayDest[%d] = %d\n", looper, intArrayDest[looper] );
        /* Prints:
        intArrayDest[0] = 1
        intArrayDest[1] = 20
        intArrayDest[2] = 43
        ...etc */
    }
    Last edited by codergeek42; October 13th, 2006 at 12:58 AM. Reason: Slight loop fix.
    ~~ Peter ~~ :: ( Who am I? ) :: ( Peter's Musings: Uploading myself, bit by bit... ) :: ( Electronic Frontier Foundation ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key ) :: ( About me on the WIki )
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2004
    Posts
    1,676
    Rep Power
    133
    Originally Posted by codergeek42
    An array variable in C is nothing more than a const pointer to the base address (0th element) of a fixed-size block of sequential memory.
    Sorry, this remark screams FAQ!
    http://c-faq.com/aryptr/constptr.html

    Comments on this post

    • codergeek42 agrees
    Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
  10. #6
  11. No Profile Picture
    Brony & F/OSS Advocate
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    Anaheim, CA (USA)
    Posts
    6,653
    Rep Power
    2476
    Originally Posted by Dave Sinkula
    Sorry, this remark screams FAQ!
    http://c-faq.com/aryptr/constptr.html
    Neat. Thanks for the clarification. :)
    ~~ Peter ~~ :: ( Who am I? ) :: ( Peter's Musings: Uploading myself, bit by bit... ) :: ( Electronic Frontier Foundation ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key ) :: ( About me on the WIki )

IMN logo majestic logo threadwatch logo seochat tools logo