Thread: structure copy

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Question structure copy

    I have been working on a home work assignment for a while. Now I am trying to copy a structure to another structure.

    The class book says that you can copy a structure using =. I cannot get that method to work.

    This is my structure.

    Code:
    #define MAXWORDS 1000
    
    typedef struct
    {
    	char Words[88];
    
    }UniqueWords;
    I have filled Word and now want to copy that contents to WordCopy.

    Code:
    UniqueWords Word[MAXWORDS];
    UniqueWords WordCopy[MAXWORDS];

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    So just iterate over every element in Word and do a strcpy of Word[i].words into WordCopy[i].words.

    Although, I don't understand why WordCopy[i] = Word[i] does not cut it for you.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Works for me.
    Code:
    #include <stdio.h>
    #include <string.h> 
    
    struct prod {
    	int height;
    	char name[5];
    };
    	
    
    int main(int argc, char *argv[]){
    	
    	int i, j ;
    	struct prod word[5] ; 
    	struct prod wordcopy[5] ; 
    	
    	for (i = 0 ; i < 5 ; i++) { 
    		word[i].height = i+1 ; 
    		strcpy(word[i].name,"Dino") ; 
    	}
    	
    	for (i = 4, j = 0 ; i >= 0 ; i--, j++ ) { 
    		wordcopy[i] = word[j] ;
    	}
    
    	for (i = 0 ; i < 5 ; i++) { 
    		printf( "height: word=%d, wordcopy=%d\n", word[i].height, wordcopy[i].height) ; 
    		printf( "name  : word=%s, wordcopy=%s\n", word[i].name, wordcopy[i].name) ; 
    	}
    
    	return 0;
    }
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    Just use the = operator.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct {
    	char Words[88];
    } UniqueWords;
    UniqueWords w0={"HEY"}, w1 = {"Hello Struct Copy!"};
    int main (void) {
        printf ("w0.Words = \"%s\"\n",w0.Words);
        printf ("w1.Words = \"%s\"\n",w1.Words);
        printf ("Swapping structs...\n\n");
        w0 = w1;
        strcpy (w1.Words,"Goodbye.");
        printf ("w0.Words = \"%s\"\n",w0.Words);
        printf ("w1.Words = \"%s\"\n",w1.Words);
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    China
    Posts
    12
    As the book said, you can certainly copy a structure like this:
    Code:
    WordCopy[i]=Word[i];
    Maybe you copied the array of structures instead of the structure itself like this:
    Code:
    WordCopy=Word;
    Of course, it is wrong.

    sorry about my poor English.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    just "=" is ok for each array element;
    you can also use memcpy, memcpy(WordCopy,Word,MAXWORDS);

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If you want to copy array of struct, you can use either loop and assign or just use memcpy() function.

    Code:
    for(i = 0; i < n; i++) {
        src[i] = dst[i];
    }
    // or using memcpy
      memcpy(src,dst, n * sizeof(*src) );

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Thank you all for your help. I will be able to move onto the next step of this assignment.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 11:20 AM
  3. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  4. using a structure..
    By jerrykelleyjr in forum C++ Programming
    Replies: 24
    Last Post: 02-16-2005, 12:51 PM
  5. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM