This declaration
char s4[5]="ABCDE";
is valid in C but is not valid in C++.
The problem with this declaration is that the array s4
does not contain a string. In C "a string" means "a null-terminated sequence of bytes", and s4
does not have a space to accommodate the terminating zero character '\0'
of the string literal "ABCDE"
used as an initializer.
Thus calling the function strstr
that expects that the both arguments will be pointers to strings results in undefined behavior.
Either write
char s4[6]="ABCDE";
or even better
char s4[]="ABCDE";
Here is a demonstrative program
#include <stdio.h>
#include <string.h>
int main(void)
{
char s3[12] = "ABCDEISHERO";
char s4[] = "ABCDE";
char* p = strstr( s3, s4 );
if ( p ) printf( "%s\n", p );
return 0;
}
The program output is
ABCDEISHERO
because the returned by the call of the function strstr
pointer p
points to the beginning of the character array s3
.