problem in a string copy function

Discussion in 'C' started by Unitedroad, Apr 30, 2007.

  1. Unitedroad

    Unitedroad New Member

    Joined:
    Apr 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I am a newbie with pointers. I have made this program which has a function that takes one string pointer as the arguement and copies the values stored at the location it points to , to another pointer and then returns that second pointer. This is the source code :
    Code:
    #include <stdio.h>
    char *StringCopy(char * NewString1);
    main() {
        char String1[80] = "How are you doing?";
        char *StringPointer1, *StringPointer2;
        StringPointer1 = String1;
        StringPointer2 = StringCopy(StringPointer1);
        puts(StringPointer2);
    }
    char *StringCopy(char *NewString1) {
        char *NewCopy = NewString1;    //a new character pointer created and initialised to the pointer from formal parameter
        
        char NewString[80];            //a new character string defined  so as to initialise a new character pointer
        
        char *NewString2 = NewString;  //a new character pointer initialised to the sting defined in previous line
        
        char *NewCopy2 = NewString2;   //another pointer defined and made to the location pointed to the pointer defined and initalised in previous line
        
        while(*NewCopy!='\0') {        
            *NewCopy2++ = *NewCopy++;
        }
        *NewCopy2 = '\0';
        return NewString2;
    }
    I think it should work but it giving some gibberish output and I am unable to find out why.\

    It works fine when I use puts(NewString2) in StringCopy() fiunction itself.

    I am using gcc , I have triend running this program on windows and Mac OS X and I got the problm both times.

    I hope someone can help me out
     
    Last edited by a moderator: Apr 30, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    The pointer you return is valid, but the area to which it points, NewString [80], being local to StringCopy, has been deallocated and (most likely) reused. You need a persistent place to hold the copy. You can get this from the heap, or you can put it in the caller (main) and pass a pointer to it.

    I notice that poor Shabbir has had to add code tags to keep your code from being unremittingly ugly and unreadable. Do you think it would be polite to read the "Before you make a query" thread, in return for the free help?
     
  3. Unitedroad

    Unitedroad New Member

    Joined:
    Apr 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi thanks alot for the help , it has helped me in further grasp the concept of pointers. I am sorry for being careless with my post and I will take car from now on.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice