C function strcpy() randomly change one character

Viewed 137

I'm learning string functions in C and I can't seem to get my teacher's demo using strcpy() to remove white spaces to work on my machine while it works perfectly fine for him. Here's the source code:

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

int main()
{
    char s[20]="abc   def   ghi";
    char *ptr = strstr(s, "  ");
    while(ptr!=NULL)
    {
        printf("Before trim: %s\n", s);
        strcpy(ptr, ptr+1);
        printf("After trim : %s\n", s);
        printf("\n");
        ptr = strstr(s, "  ");
    }
    return 0;
}

Expected result:

Before trim: abc   def   ghi
After trim : abc  def   ghi

Before trim: abc  def   ghi
After trim : abc def   ghi

Before trim: abc def   ghi
After trim : abc def  ghi

Before trim: abc def  ghi
After trim : abc def ghi

Actual result:

Before trim: abc   def   ghi
After trim : abc  def   ghi

Before trim: abc  def   ghi
After trim : abc def   ghi

Before trim: abc def   ghi
After trim : abc def  hhi

Before trim: abc def  hhi
After trim : abc def hii

I've searched for this error and learned that strcpy() is unsafe because it may cause overflow or undefined behavior. Most of the search I've read is about the destination buffer is not large enough. I don't know the keyword of this strange behavior. Can someone please explain to me what am I doing wrong? Thank you in advance!

1 Answers
char *strcpy( char *restrict dest, const char *restrict src ); //(since C99)

Copies the null-terminated byte string pointed to by src, including the null terminator, to the character array whose first element is pointed to by dest.

The behavior is undefined if the dest array is not large enough. The behavior is undefined if the strings overlap. The behavior is undefined if either dest is not a pointer to a character array or src is not a pointer to a null-terminated byte string.

https://devdocs.io/c/string/byte/strcpy

This is the kind of thing a teacher should be careful with, though I must say that compiling the code with several versions of MSVC, clang and gcc I was never able to reproduce the described actual result.

Related