1

I've just written my strcpy function but it doesn't work properly. After x amount of characters I have an infinite loop or it will copy my string but give me an error...

char    *ft_strcpy(char *dest, char *src)
{
       int i;

       i = 0;
       while (*(src + i) != '\0')
       {
            *(dest + i) = *(src + i);
            i++;
       }
       *(dest + i) = NULL;
       return (dest);
}
Tóth Attila
  • 149
  • 1
  • 1
  • 10
  • 4
    Not the source of your bug, but `\0` isn't the same thing as `NULL`. Use the former not the latter. – Lundin Sep 05 '16 at 14:22
  • 1
    Can we see how you call this function ? – Garf365 Sep 05 '16 at 14:25
  • 2
    FYI, `*(dest + i)` is exactly the same as `dest[i]`. Use compact and intuitive notation. – n. m. could be an AI Sep 05 '16 at 14:27
  • 1
    Note that [strcpy is considered to be dangerous and should not be used](https://stackoverflow.com/questions/1258550/why-should-you-use-strncpy-instead-of-strcpy). Maybe your input string doesn't contain a '\0' terminator? – Jens Mühlenhoff Sep 05 '16 at 14:27
  • @Garf365 below is how i call the function – Tóth Attila Sep 05 '16 at 14:30
  • 1
    If you want help, you **must** present the entire buildable program, the exact input, the exact output, and/or the exact error message you're getting. Words like "with some input there is bad output or an error" convey **no** useful information. – n. m. could be an AI Sep 05 '16 at 14:31
  • 1
    @JensMühlenhoff: Errr... **what**? How are you constructing the linked Q&A to mean "strcpy is considered dangerous and should not be used"? If the input is a *string*, it *is* (by definition) terminated. Problems with `strcpy` arise only if a) you are feeding it something that is *not* a string (which it is not defined for), or b) you haven't checked if the target buffer is big enough. Checking for that condition is as easy as checking if `strncpy()` actually left you with a *string* in the target buffer (which it might not have). All of which is of course OT for the question... – DevSolar Sep 05 '16 at 14:33
  • 2
    @JensMühlenhoff "considered dangerous" by whom? `strncpy` is *more* dangerous because it gives a false sense of security. If I got a nickel each time I saw a totally wrong use of strncpy I would have, oh, maybe $100 by now. – n. m. could be an AI Sep 05 '16 at 14:38
  • @DevSolar Ok, it is *safe when used properly*, I was just thinking about the "endless loop" part and thought the program might be reading past the input. The problem is that OP didn't post an MCVE. – Jens Mühlenhoff Sep 05 '16 at 14:39
  • @n.m. I didn't want to advocate use of `strncpy`, you are of course correct. I just wanted to point out that the input must be valid (which is true for most C functions I guess, garbage in .. garbage out). – Jens Mühlenhoff Sep 05 '16 at 14:41
  • One obvious error is the use of a (signed) integer as the loop variable. Use `size_t` or just pointer arithmetic. – EOF Sep 05 '16 at 14:42
  • 1
    "Safe if used properly", that is C (and C++) in a nutshell. ;-) – DevSolar Sep 05 '16 at 14:51

2 Answers2

3

In this example :

 char a[] = "abc";
 char b[] = "xyzsadasdasdadsi";

You're trying to put 17 characters into string char *a, which is supposed to store only 4 of them.

You can not do this. You must use a new string in which you allocated enough memory to store the characters needed, using strlen(), for example :

char *new = malloc(sizeof(char) * (strlen(src) + 1));

Plus, you should also correct your function :

char    *ft_strcpy(char *dest, char *src)
{
   int i;

   i = 0;
   while (*(src + i) != '\0')
   {
        *(dest + i) = *(src + i);
        i++;
   }
   *(dest + i) = '\0';
   return (dest);
}
souki
  • 1,305
  • 4
  • 23
  • 39
0

Maybe you should allocate memory manually with malloc and terminate the string with '\0' (not NULL which is different).

Like

dest = (char *)malloc(sizeof(char) * strlen(src) + 1));
dest[strlen(src)] = '\0';
Oolira
  • 1
  • 1