Member-only story
Match function: Using C++ pointers, create a method that returns true if and only if its two C string arguments have exactly same text
Question
The match function is supposed to return true if and only if its two C string arguments have exactly same text.
Explanation
Within the match method, 2 pointer variables are declared for str1 and str2.
‘const char *p1 = str1;’ line will declare a constant (cannot manipulate) variable with the memory address of the first element.
In the while loop statement, ‘*’ is used to get the element of where the pointer is pointing now, which starts with pointing to the first element. Within the while statement, we check the values of the pointers, again using the symbol ‘*’ which returns the value of where the pointer is. pointing.
Inside the while loop, after the if statement, the pointer will go to the next element by using these lines line ‘p1++’ and ‘p2++’. The pointer will keep going to the next element until it finds a difference between the 2 arrays.
Solution
bool match(const char str1[], const char str2[]){const char *p1 = str1;const char *p2 = str2;while (*p1 != 0 && *p2 != 0) // zero bytes at ends{if (*p1 != *p2){ // compare…