Pointer help.

Hi,
I am very new to C++ and have been teaching myself from a book; got a little confusing when my book talked about returning a pointer with a example program which helped a little just don't fully understand it.

Ok, the program below; the function get_substr() searches a string for a substring. It returns a pointer to the first matching substring, if no match is found, a null pointer is returned.

What I don't understand is the program output is
Substring found: three four
mean I thought the program searches for the substring "three" and returns that if the word "three" is found, why is the word "four" there?
Also, I'm very confused on the pointers in the for and while loop and how they work:
1
2
3
4
5
6
7
8
9
10
for(t=0; str[t]; t++) {
		p = &str[t]; // reset the pointers.
		start = p;
		p2 = sub;
		while(*p2 && *p2==*p) { //check for substring.
			p++;
			p2++;
		} /* If at end of p2 (i.e., substring), then a match has been found. */
		if(!*p2) return start; // return pointer to beginning of substring
	}


I have posted the whole program below if it is needed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Return a pointer.

#include <iostream>
using namespace std;

char *get_substr(char *sub, char *str);

int main() {
	char *substr;

	substr = get_substr("three", "one two three four");

	cout << "Substring found: " << substr;

	cout << "\n\n";

	return 0;
}

// Return pointer to substring or null if not found.
char *get_substr(char *sub, char *str) {
	int t;
	char *p, *p2, *start;

	for(t=0; str[t]; t++) {
		p = &str[t]; // reset the pointers.
		start = p;
		p2 = sub;
		while(*p2 && *p2==*p) { //check for substring.
			p++;
			p2++;
		} /* If at end of p2 (i.e., substring), then a match has been found. */
		if(!*p2) return start; // return pointer to beginning of substring
	}
	return 0; // no match found.
}


Thank you,
Luke!
The value returned is just a pointer to a single character, at the position in str where the substring was found. What the user chooses to do with that information in a matter of personal preference.

In this case, at line 13, the contents of the string, starting from the position pointed to, will be displayed. Printing stops when the null character is reached at the end of the string.
 
cout << "Substring found: " << substr;



The function here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Return pointer to substring or null if not found.
char *get_substr(char *sub, char *str) {
    int t;
    char *p, *p2, *start;

    for(t=0; str[t]; t++) {
        p = &str[t]; // reset the pointers.
        start = p;
        p2 = sub;
        while(*p2 && *p2==*p) { //check for substring.
            p++;
            p2++;
        } /* If at end of p2 (i.e., substring), then a match has been found. */
        if(!*p2) return start; // return pointer to beginning of substring
    }
    return 0; // no match found.
}

This code in several places depend upon the fact that a c-string must end with a zero byte, known as a null terminator.

for(t=0; str[t]; t++) This for loop steps through the string one position at a time, and will halt when the condition str[t] is false, that is, the byte at position t is zero.

Within that loop is contained this loop:
1
2
3
4
        while(*p2 && *p2==*p) { //check for substring.
            p++;
            p2++;
        }

Here p2 is pointing to a character in the substring, and p is pointing to a character in the string being searched. It compares the corresponding characters and continues while both these conditions are true:
*p2 - that is, the current character in the substring is not zero
*p2==*p - the characters are equal.
When that loop ends, it means either of those conditions might be true. So then there is a check, to see which one caused the loop to end. If the character pointed to by p2 is zero, then each character in the substring must have matched a character in the string, the search was successful, so the value of start (current position in the string is returned as the result.
Last edited on
The function is working correctly.

In main, you're displaying substr; this will display all the characters until the end of the string.

This is why if you search for "one" instead, it'll display "one two three four".

Why don't you try this instead?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

using namespace std;

char *get_substr(char *sub, char *str);

int main()
{
    char *substr;
    char word[] = "three";
    char sentence[] = "one two three four";

    substr = get_substr(word, sentence);

    for (int i = 0; i < (sizeof(word) - 1) / sizeof(char); i++)
    {
        cout << *(substr + i);
    }

    cout << "\n\n";

    return 0;
}

char *get_substr(char *sub, char *str)
{
    int t;
    char *p, *p2, *start;

    for(t = 0; str[t]; t++)
    {
        p = &str[t];
        start = p;
        p2 = sub;

        while(*p2 && *p2==*p)
        {
            p++;
            p2++;
        }

        if(!*p2)
        {
            return start;
        }
    }

    return 0;
}

I only changed main.
Yes; I understand now.

Josue-Yeah that program only displays the word "three"; thanks for the different way :)

Chervil-Thanks for explaining.




Topic archived. No new replies allowed.