0
//a function that copies one string to another
copy(char *,char*);
main()
{
    char one[20],two[20];
    printf("enter two sentences \n\n");
    gets(one);//first string
    gets(two);//second string
    copy(one,two);
    printf("%s",two);
}
copy(char *s1,char *s2)
{
    while(*s1!='\0')
    {
        s2=s1;
        s1++;
        s2++;
    }
    s2='\0';
}

what wrong with the above program ? why the string 'one' is not getting copied to string 'two'?please explain with the help of pointer

1
  • 1
    Think over why you are using the *-operator here: *s1!='\0' and do not use it here: s1=s2; and here s2='\0';. Commented Mar 14, 2015 at 9:51

2 Answers 2

2

It's because this:

s2 = s1;

changes the pointer s2 so that it points to the content of s1.

What you want to do is copy the content:

*s2 = *s1;

A decent compiler should also have given you a warning on this line:

s2 = '\0';

since you're assigning a char to a char *. It should be:

*s2 = '\0';

Enacting those changes, the function would then be (including using some, IMNSHO, better variable names):

void copy (char *from, char *to) {
    while (*from != '\0') {
        *to = *from;
        from++;
        to++;
    }
    *to = '\0';
}

Or, once your brain has been warped by several decades of C use like mine :-)

void copy (char *from, char *to) {
    while (*to++ = *from++);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Why use the do...while loop? Isn't the while loop better?
@CoolGuy, the advantage of the do.while is that it checks the terminating condition after the iteration, meaning you don't need more code at the end to copy or put a string terminator.
I know that. But isn't a while loop better in this case(as there are less characters to type)? Also, I didn't mean the while loop in the answer... I meant using while (*to++ = *from++); instead of do {} while (*to++ = *from++);.
@CoolGuy, yes, I see what you're getting at now, I'll incorporate the shorter version.
1
#include <stdio.h>
#include <string.h> /* for strchr */

void copy(const char *, char*); /* use void to return nothing */

int main(void) /* main() is not valid */
{
    char one[20], two[20];
    char *ptr;

    printf("enter two sentences \n\n");
    /* gets is deprecated, use fgets in order to avoid overflows */
    fgets(one, sizeof one, stdin);
    /* fgets leaves a trailing newline, remove it */
    if ((ptr = strchr(one, '\n'))) *ptr = '\0';
    fgets(two, sizeof two, stdin); /* why? is gonna be replaced by one */
    copy(one, two);
    printf("%s\n", two);
    return 0;
}

void copy(const char *s1, char *s2) /* s1 is not modified, use const */
{
    while(*s1 != '\0')
    {
        *s2 = *s1; /* Don't assign addresses, assign values */
        s1++;
        s2++;
    }
    *s2 = '\0';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.