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

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Copying strings using pointers


    Hi, i am trying out a simple program which uses pointer to
    copy from one array to the next.

    I face an issue.

    I have 2 arrays, source[50], destination[50]

    When i use gets(array1) to insert user input into the first array, i noticed that the input is successfully inserted into the first array.

    However, at the last 5 spaces of the 2nd array, the sentence is also present.

    Could this be something to do with addressing?

    I can solve the problem simply by doing a for loop and initializing all values of array[2] to 0.

    However, i am curious of this phenomena, and wish to understand better.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Show us the code and the user input.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    1
    Post your code because we do not see how you implemented the "program which uses pointer to
    copy from one array to the next."

    Your problem must be about using gets() or using pointers. By the way, never use pointers and assign things to them if they are not malloc() and do not forget to free()
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    > However, at the last 5 spaces of the 2nd array, the sentence is also present.
    > Could this be something to do with addressing?
    It seems more likely to have everything to do with buffer overflow.

    > When i use gets(array1) to insert user input into the first array,
    You allocated 50 bytes, how much did you type in - more than 50 chars perhaps?

    How does gets know how much space to use? It can't!
    NEVER use gets()
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    1
    ...Unless you use sscanf. :D
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    20
    Rep Power
    0
    C profram- string copying using pointers
    #include<stdio.h>

    void copy_string(char*, char*);

    main()
    {
    char source[100], target[100];

    printf("Enter source string\n");
    gets(source);

    copy_string(target, source);

    printf("Target string is \"%s\"\n", target);

    return 0;
    }

    void copy_string(char *target, char *source)
    {
    while(*source)
    {
    *target = *source;
    source++;
    target++;
    }
    *target = '\0';
    }

IMN logo majestic logo threadwatch logo seochat tools logo