Thread: copying contents of array1 into array2

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    copying contents of array1 into array2

    Hi,
    I am trying to write a program that asks for a string, enters it into array1, and then copies it into array2, which is empty. The program then displays array1 and array2. The problem is that I can't seem to get the first array to enter into the second. Im trying to do this using just pointers with arrays. I don't know any of the string functions yet, like strncpy() or similar ones. The section in red is where I try to copy array1 to array2. Probably makes no sense, but I'm stuck. Any help would be appreciated.
    Code:
    #include <stdio.h>
    
    main()
    
    {
          char string1[51],
               string2[51],
               *letter,
               *letter2;
               
               printf("\nPlease enter a string: ");
               gets(string1);
               
               /*PRINT STRING1*/
               for(letter = string1; *letter != '\0'; ++letter)
               printf("%c" , *letter);
               
               /*ASSIGN STRING1 TO STRING2*/
               for(letter2 = string2; *letter2 != '\0'; ++letter2) 
               string2[*letter2] = *letter;
               
              putchar('\n');
              
               /*PRINT STRING2*/
               for(letter2 = string2; *letter2 != '\0'; ++letter2)
               printf("%c" , *letter2);
               
               
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    for(letter2 = string2, letter1 = string1; *letter1 != '\0'; ++letter2, ++letter1) 
    	*letter2 = *letter1;
    Assign both accessory pointers to the beginning of those strings. Then, while we're not at the end of the existing string we entered, make them equal, and increment both. This is often shortened to:

    Code:
    while(*d++ = *s++);
    For a function like strcpy, copying the dest 's' from source 's'
    Last edited by Tonto; 04-04-2006 at 11:54 PM.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Thanks tonto,
    I didn't know you could write for loops like that. The output was correct, but it had some garbage characters at the end of it that I cant figure out.
    Code:
    Please enter a string: asdfjkl;
    asdfjkl;
    asdfjkl;↑εÉ|p♣æ|........m♣æ|▐┬┬w

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    answer

    Yes , thats true. Because you did'nt null terminated string2.

    after the for loop
    Code:
    for(letter2 = string2, letter = string1; *letter != '\0'; ++letter2, ++letter) 
    	*letter2 = *letter;

    Notice that when you print string2, you test it until we reach a null.
    we want to null terminate string2. so, add this
    Code:
    *letter2 = '\0';

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    That's because the loop stopped before transferring the ending \0 from the source string. Add:
    Code:
    while(*d++ = *s++);
    *d = '\0';
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    I can't see why the \0 gets cut off. Is it because this condition:
    Code:
    *letter != '\0'
    leaves the \0 non inclusive?

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    During the copy from one string to the other, you loop until you reach a NULL. Once you hit that NULL, you stop copying (the NULL does not get copied); so... you must then take the extra step to properly NULL terminate the new string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by richdb
    I can't see why the \0 gets cut off. Is it because this condition:
    Code:
    *letter != '\0'
    leaves the \0 non inclusive?
    '\0' is a scape character btw, takea look in strcpy function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String copying
    By NegativeSpace in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2008, 03:14 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Defining an initialized int table?
    By Quantum1024 in forum C Programming
    Replies: 10
    Last Post: 03-30-2005, 07:25 AM
  4. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  5. needs help copying arrays
    By RedRattler in forum C Programming
    Replies: 3
    Last Post: 04-04-2003, 11:34 PM