0

I can release this using arrays, but i want to make it working with pointers.

 char *ft_strcpy(char *dest, char *src)
    {
        while (*src)
        {
            *dest = *src;
            dest++;
            src++;
           // return dest; i want 
        }
        *dest = '\0';
        return (dest);
    }
2
  • 4
    Consider at what point do you have "the whole string" available. When do you lose it? Think. Commented Sep 25, 2018 at 9:46
  • You could look at the hundreds of other implementations of strcpy already present on SO and compare it with your code, to see what's different. Commented Sep 25, 2018 at 10:54

3 Answers 3

2

You are losing your string as you increment dest. At the end dest is pointing to a location which holds '\0'. You can use a temporary variable for modification while dest is still pointing to the beginning of the allocated memory.

char *temp = dest;
while (*src)
{
    *temp = *src;
    temp++;
    src++;
}
*temp = '\0';
return (dest);
Sign up to request clarification or add additional context in comments.

1 Comment

@Vladimir Try to understand the following Program. Replace dest[i] = '\0'; with tmp[i] = '\0'; and see what happens.
0

You miss the main C principles of pointer usage. When you are using dest++; you have move the pointer to next byte. In case you have return latest dest value - you will get pointer to end of your string instead first. Another one - you don't need to return pointer, all pointer operations changing data into base string used like dest parameter in your example. So when you will finish the function you can get pointer used as dest parameter in parent function and will see your changes complete.

In case you still want some construction of function with return pointer do next:

char *ft_strcpy(char *dest, char *src)
    {
        char * retValue = dest;
        while (*src)
        {
            *dest = *src;
            dest++;
            src++;
           // return dest; i want 
        }
        *dest = '\0';
        return (retValue);
    }

Than you can get the pointer to first byte in the string as your return value.

Comments

0

As well answered by others concerning returning the original dest as below,

char *ft_strcpy(char *dest, char *src) {
  char *original_dest = dest; // add
  while (*src) {
    *dest++ = *src++;
  }
  *dest = '\0';
  // return dest; 
  return original_dest; 
}

It is interesting to think about the impact of overlapping strings. The below will cause ft_strcpy() to loop on and on until code accesses outside buf[] leading to undefined behavior rather than a hoped for "hehello". The while (*src) { loop, in this case, over wrote the null character.

char buf[10] = "hello";
ft_strcpy(buf + 2, buf);

Such calls as above are pathological, yet allowed.

The standard strcpy() disallows this type of call with restrict. Simplistically, this means s1,s2 shall not overlap.

char *strcpy(char * restrict s1, const char * restrict s2); 

How could code be written to handle overlapping strings?

Determine the length before copying.
Copy with memmove() which handles overlapping buffers.

char *ft_strcpy2(char *dest, const char *src) {
  size_t sz = strlen(src) + 1;
  memmove(dest, src, sz);
  return dest; 
}

int main(void) {
  char buf[10] = "hello";
  puts(ft_strcpy2(buf + 2, buf));
  puts(buf);
}

Output

hello
hehello

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.