1

I have read the documentation of strcat() C library function on a few websites.

I have also read here: Does strcat() overwrite or move the null?

However, one question is still left - can strcat() function be used to override the characters in the destionation string (assume that dest string has enough space for the source string, so there will be no errors)?

I ran the following code and found that it doesn't have the ability to override the dest string's characters...

char dest[20] = "Hello World";
char src[] = "char";
strcat(dest+1, src);
printf("dest: %s", dest);

Assume that the goal is to have a destination string that contains: "Hchar World!"

(I know that strcat() also copies the NULL characters('\0') to the dest string, so if printf() function is called, it should print Hchar, as I mistakenly thought would happen...).

Is that a possible task to do with strcat()? If not, is strcpy() the answer to the question?

If there is an assignment of '\0' (NULL character) in the middle of the string, for example, will strcat() always treat the first '\0' (NULL character) it meets? I mean, If I had:

 char str[] = "Hello";
 str[2]= 0;
 strcat(str, "ab");

I just want to be sure and clarify the misunderstanding. I will be glad to read explanations.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Modern
  • 75
  • 2
  • 9
  • `strcat` will always append to the existing string. `strcpy` will also copy the `\0`, so you would get the result `"Hchar"` (in fact it is `"Hchar\0World"`) – Bodo Dec 20 '21 at 10:04
  • @Bodo Thanks for replying. You meant I would get "Hchar\0World" if I use strcpy()? – Modern Dec 20 '21 at 10:07
  • try it in a debugger, e.g. https://onlinegdb.com/Be3_3QfEN Set a breakpoint at the `printf` line and check the contents of `dest` – Bodo Dec 20 '21 at 10:17

2 Answers2

1

strcat will write src string at the end of dst. If you want to override dst with strcat, you first need to make dst "end" where you want to override it.

Take a look at this code sample:

#include <stdio.h>
#include <string.h>

int main()
{
    char dst[20] = "Hello world";
    char src[] = "char";
    
    dst[1] = '\0';
    strcat(dst, src);
    printf("%s\n", dst);
    return (0);
}

However, this is not the aim of strcat, and as said in the comments, the use of strcpy would be more appropriate here.

#include <stdio.h>
#include <string.h>

int main()
{
    char dst[20] = "Hello world";
    char src[] = "char";
    
    strcpy(dst + 1, src);
    printf("%s\n", dst);
    return (0);
}
lfalkau
  • 896
  • 7
  • 21
1

As noted in the comments, the strcat function will always (attempt to) append the string given as its second argument (traditionally called src) to that given as its first (dest); it will produce undefined behaviour if either string is not null-terminated or if the destination buffer is not large enough.

The cppreference site gives better documentation (for both C and C++) than the website you linked. From that site's strcat page:

(1) … The character src[0] replaces the null terminator at the end of dest. The resulting byte string is null-terminated.

And:

Notes

Because strcat needs to seek to the end of dest on each call, it is inefficient to concatenate many strings into one using strcat.

So, in the code you show, calling strcat(dest+1, src); has the same effect as calling strcat(dest, src);. However, calling strcpy(dest+1, src); will produce the result you want (printing Hchar).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Thanks for answering. Does it mean that whatever address that is in the range of dest string will be taken as the first argument in the strcat() function, it will always treat only the address of the first '\0' it recognizes in the dest string, and there the source string will be concatenated? – Modern Dec 20 '21 at 10:28
  • @Modern Yes. It will look for the first `nul` (zero) byte located **at or after** the address you give as the first argument, and copy the second argument to memory starting at that location. – Adrian Mole Dec 20 '21 at 10:29
  • Thank you for replying. Just to make sure - if there is a null character before the address I wrote as the first argument, the strcat() will not recognize it, is that right? – Modern Dec 20 '21 at 10:32
  • @Modern Correct. What is in memory/buffers **before** the address passed as `dest` is not relevant to the `strcat` function. – Adrian Mole Dec 20 '21 at 10:33
  • Thank for you the explanation. Is it right about strcpy() too (what you wrote about the given first argument)? – Modern Dec 20 '21 at 10:34
  • @Modern The `strcpy` function will copy the `src` (2nd argument) to the *actual* location specified as the `dest` (1st argument), overwriting what is there and subsequent bytes, up to and including the terminating `nul` in the `src`. – Adrian Mole Dec 20 '21 at 10:38