Copying struct

This is a discussion on Copying struct within the C Programming forums, part of the General Programming Boards category; If A and B are 2 POINTER declarations of a particular struct type, to copy one address to another, it ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    Copying struct

    If A and B are 2 POINTER declarations of a particular struct type, to copy one address to another, it would be alright to do this (correct me if i am wrong) :

    A = B;

    is there anyway of copying the whole structure and contents of B to A without assigning the same address to A?
    Only by the cross are you saved...

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Are A and B going to be pointers still?

    --edit--
    Not if they are pointer declarations. But seeing how they point to the same struct object, you can access them using either pointer.
    Last edited by stumon; 08-10-2003 at 08:54 PM.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    *A = *B;

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    I remember reading somewhere once that assigning one structure to another of the same type is not guaranteed to copy the values of each of the members to the other and is compiler specific.

    Can someone confirm/validate this?
    Beware the fury of a patient man.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    Don't worry. Just checked Comp.Lang.C FAQ and found this:

    Q: I heard that structures could be assigned to variables and passed to and from functions, but K&R1 says not.

    A: What K&R1 said was that the restrictions on structure operations would be lifted in a forthcoming version of the compiler, and in fact structure assignment and passing were fully functional in Ritchie's compiler even as K&R1 was being published. Although a few early C compilers lacked these operations, all modern compilers support them, and they are part of the ANSI C standard, so there should be no reluctance to use them.
    The more you know!
    Beware the fury of a patient man.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    I think you cannot copy contents of A and B just by assigning the address of A to B. You have to assign each and every field present in A to B. In Short, if A and B are two structures you cannot copy contents of A to B like this:B=A.
    Saravanan.T.S.
    Beginner.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    I think you cannot copy contents of A and B just by assigning the address of A to B. You have to assign each and every field present in A to B. In Short, if A and B are two structures you cannot copy contents of A to B like this:B=A.
    Read the above.
    Beware the fury of a patient man.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    wat about *A = *B ?

    this still assigns the address over?
    Only by the cross are you saved...

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    quote:
    --------------------------------------------------------------------------------

    I think you cannot copy contents of A and B just by assigning the address of A to B. You have to assign each and every field present in A to B. In Short, if A and B are two structures you cannot copy contents of A to B like this:B=A.

    --------------------------------------------------------------------------------

    >>Zainny wrote::::::Read the above.


    You can not do it, AFAIK. What you are talking about it in the faq, IMHO, is about passing a struct as a parameter to a function, which is definitely not A = B; to create a copy of the structure using "pass by value" is legal, however not very desirable in real time computing since the structures tend to be big. But within a function, if you want to create a copy of a structure, you just can not say A = B;

    However, in this context A and B are pointers and A = B is legal but it only assigns the address pointed to by B to be pointed to by A. Thats all it does.

    Anoop.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    >>wat about *A = *B ?

    >>this still assigns the address over?


    I doubt if it will even work properly. Its behaviour might be undefined at best and worst, it could be lead to a segmentation fault.

    Anoop.

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    hm...........okay, i now know that A = B won't work...
    so is there any other way to copy structs apart from copying each data member one by one?
    Only by the cross are you saved...

  12. #12
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    memcpy might work.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    >>>> hm...........okay, i now know that A = B won't work...

    It will work coz you have declared A and B as pointers. But it wont do what you want it to do.

    Any other way except copying memberwise??? in one word NO. Though if you want to do it on a large scale, then you are better off using a function to do the copy.

    The other reason, why you are looking for an easier way, I can think of is that you have too many variables in your structure. In that case you will have to do the hard work.

    Anoop.

  14. #14
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct FOOBAR
    {
      int num;
      char letter;
    };
    
    int main(void)
    {
      struct FOOBAR *testA;
      struct FOOBAR *testB;
    
      testA = malloc(sizeof(struct FOOBAR));
      if (testA == NULL)
      {
        printf("Error: Unable to allocate memory for testA!");
        exit(0);
      }
      testB = malloc(sizeof(struct FOOBAR));
      if (testB == NULL)
      {
        printf("Error: Unable to allocate memory for testB!");
        exit(0);
      }
    
      testA->num = 0;
      testA->letter = '\0';
    
      testB->num = 10;
      testB->letter = 'c';
    
      *testA = *testB;
    
      printf("The num is : %d\n", testA->num);
      printf("The letter is : %c\n", testA->letter);
    
      return 0;
    }
    I might not be following this properly, but are you saying that this is not valid?

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    >>>memcpy might work.

    after typecasting?

    highly unsafe. and then worry about padding '\0' at the end of each string.

    Anoop.

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

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 03:16 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 10:57 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 03:56 PM

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