Hello folks,
I tried writing my own function on string copy and this is what i managed to do.Though it somehow works, I have got a great problem with the manipulation of pointers. Please explain or modify it for me. Thank You.

#include <stdio.h>
#include <stdlib.h>

int main()
{
char copy[30],original[30];
printf("Please enter a string\n");
scanf ("%s",original);
printf("Please enter a new string\n");
scanf ("%s",copy);
char* copyPtr;
char* originalPtr;
copyPtr = copy;
originalPtr = original;
printf ("You have entered\nOriginal = %s\nCopy = %s",original,copy);
// int i;
while (*originalPtr != '\0')
{
copyPtr = originalPtr;
originalPtr--;
}
originalPtr = original;
printf ("\n\nNow this is what we have\nOriginal = %s\nCopy = %s",originalPtr,copyPtr);
return 0;
}

Recommended Answers

All 10 Replies

>copyPtr = originalPtr;

That is just changing the pointer, not the contents of the arrays. You have to add * to each, such as *copyPtr++ = *originalPtr++; You also have to increment both pointers. So the loop becomes

while(*originalPtr)
{
    *copyPtr++ = *originalPtr++;
}
*copyPtr = 0; // null-terminate it

Also scanf would terminate on encountering whitespace so use fgets instead.

Thanks faxo and ancient dragon. Ancient Dragon i tried out your code however no value was printed out. Faxo, I also think its a good idea to use the fgets. However it should have been just gets since i'm not dealing with files over here.

Thanks faxo and ancient dragon. Ancient Dragon i tried out your code however no value was printed out. Faxo, I also think its a good idea to use the fgets. However it should have been just gets since i'm not dealing with files over here.

Not sure what you mean by 'should have been just gets since i'm not dealing with files over here'...It really doesn't matter, gets(char*) is dangerous...

BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

Thanks gerard4143.

Guys as a result of your help, I've now modified the code and this is what I managed to do. It is working perfectly. Thanks.

#include <stdio.h>
#include <stdlib.h>
void stringcopy (char *,char *);
int main()
{
   char string1 [20];
   char string2 [20];
   printf ("Please enter a string\n");
   fgets (string1);
   printf ("Please enter a string\n");
   fgets (string2);
   stringcopy(string1,string2);
   printf ("string 1 = %s\nstring 2 = %s",string1,string2);
   return 0;
}

void stringcopy (char* copyPtr, char* originalPtr)
{
    while (*originalPtr != '\0')
{
   *copyPtr++ = *originalPtr++;
}
   *copyPtr = 0;
}

>> fgets (string1);
Your compiler should have given you an error on that line because fgets() requires three parameters.

Okay, so what should I have used instead of fgets().

fgets() is the correct function, you just didn't use it correctly. google for that function and you will find out how to use it properly. Yes I can easily tell you how but you need to learn how to find it yourself so that next time you will know how to do it.

fgets() is the correct function, you just didn't use it correctly. google for that function and you will find out how to use it properly. Yes I can easily tell you how but you need to learn how to find it yourself so that next time you will know how to do it.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.