0

I can not understand how this code will execute. As far as I know, strstr() will return a pointer to the first letter of the matching string. So, how can we do char *newx=p+strlen(str1); when p is a pointer and strlen() returns an integer value?

p=strstr(str2,str1);
if(p){
   char *newx=p+strlen(str1);
   strcpy(t,newx);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 4
    If you don't know what adding an integer to a pointer does, it will be good to go through a textbook to understand the fundamentals of the language. Getting an answer to this specific question isn't going to be helpful in the long run. For a list of good books, check out [the definitive C++ book guide](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – R Sahu Mar 04 '19 at 05:13
  • You should learn pointer arithmetic. Have a look at https://www.learn-c.org/en/Pointer_Arithmetics – Michael Veksler Mar 04 '19 at 05:36

2 Answers2

1

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" 
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • what about if(p) bacuase p is a pointer and as much as i know p should be an integer to execute the statement ..in what condition if(p) will not execute – Abdaal Ahmad Mar 04 '19 at 07:57
  • @AbdaalAhmad a pointer is implicitly convertable to a boolean, where null is evaluated as false and anything else is evaluated as true. All of this stuff is covered by [any decent book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), I suggest you start reading them – Remy Lebeau Mar 04 '19 at 15:13
0

This is a question regarding pointer arithmatic. I suggest you read https://www.tutorialspoint.com/cplusplus/cpp_pointer_arithmatic.htm for more info on pointer arithmatic.

For this question, the clarification is, when you add the size of the str1 to newx, it automatically identifies that you're incrementing a char pointer and hence the address needs to be incremented by sizeof(char) * strlen(str1)

Hope this clarifies on your issue and highly recommend you to read through pointer arithmatic.

Destructor
  • 523
  • 1
  • 3
  • 13