2

Main question is: should I ever use strcpy (from cstring library)?

I often use strcat instead of strcpy in pgms like:

char arr[10];
arr[0] = '\0';
strcat(arr, "hey!"); // alternatively strcpy(arr, "hey!");

I cannot think of cases where strcpy could not be replaced by strcat. Are there any?

imreal
  • 10,178
  • 2
  • 32
  • 48
hartmut
  • 934
  • 12
  • 25
  • 2
    When one does not initialise the string with a null string in the first place – Ed Heal Jan 04 '16 at 17:31
  • 2
    I you are going to make that code obfuscation a bad habit, you can use strcat instead of strcpy –  Jan 04 '16 at 17:32
  • 1
    @DieterLücking - Try switching the last bit – Ed Heal Jan 04 '16 at 17:34
  • 8
    `strcpy` and `strcat` carry different semantics, although by coincidence they may produce identical results. Code is more readable if you convey as much information as you can. Using `strcpy` to copy, and `strcat` to append strings is recommended. – IInspectable Jan 04 '16 at 17:39
  • 3
    Q: "I often use `-=` and `+=` instead of ordinary assignment. Eg. `i -= i, i += 42;` I think I could always replace `i = 42;` with the above. Can you think of a reason I shouldn't?". A: Yes, I can; several. But if you're seriously asking that question, my answers are probably going to fall on deaf ears. – rici Jan 04 '16 at 17:40
  • 3
    Can you explain *why* you like to use `strcat` when `strcpy` would do? – Keith Thompson Jan 04 '16 at 17:41
  • Note that `strcat()` is a very dangerous function as it can lead to very inefficient code. Using multiple `strcat()`s on the same string is often done and it's very inefficient. – Iharob Al Asimi Jan 04 '16 at 17:43
  • Thank you. Crystal clear now. – hartmut Jan 04 '16 at 18:00
  • "*I often use `strcat` instead of `strcpy` ...*" Why? – alk Jan 04 '16 at 18:10

2 Answers2

3

Are there any cases where strcpy can't be replaced by strcat (after setting the initial character of the target to '\0')? I don't think so.

Does that mean you should always use strcat when strcpy would do the job? Absolutely not.

strcpy makes no assumptions about the initial contents of the target array.

strcat requires the target to contain a terminating '\0' null character -- and if it's not in the initial position, it will scan for it, to and possibly beyond the end of the array.

It's true that

strcpy(dest, source);

is equivalent to

dest[0] = '\0';
strcat(dest, source);

so you can always use strcat instead of strcpy. That doesn't mean you should.

strcpy is commonly used to replace the contents of an array:

char s[100] = "hello, world";
...
strcpy(s, "goodbye, cruel world");

Replacing that last line with:

s[0] = '\0';
strcat(s, "goodbye, cruel world");

would just make the code more verbose and more difficult to read.

If you want to copy a string into an array, use strcpy. If you want to concatenate a string onto an existing one, use strcat.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
2

From doc of strcat:

Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

so destination has to be null terminated. This is not required for strcpy.

And as you do it, indeed it would be more readable to use strcpy in your case, rather than manually assigning 0 to first element of array and using strcat.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90