This is simple pointer arithmetic.
Adding an integer to a pointer increments the pointer by the specified number of elements.
Say you have a pointer T *ptr
. When you do something like this:
T *ptr2 = ptr + N;
The compiler actually does (an optimized) equivalent of this:
T *ptr2 = reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(ptr) + (sizeof(T) * N));
So, the code in question is using strstr()
to search the str2
string for a substring str1
and get a pointer to that substring. If the substring is found, that pointer is then incremented via strlen()
to the address of the character immediately following the end of the substring, and then strcpy()
is used to copy the remaining text from that address into the t
string.
For example:
const char *str2 = "Hello StackOverflow";
const char *str1 = "Stack";
const char *p;
char t[10];
p = strstr(str2, str1); // p points to str2[6]...
if (p) { // substring found?
const char *newx = p + strlen(str1); // newx points to str2[11]...
strcpy(t, newx); // copies "Overflow"
}