Writing my own strstr function that finds multiple matches

Hello. I am trying to write my own version of the strstr() function that can find multiple matches. The function should return the number of matches found, and retStrings should contain a contiguous list of char* pointers that point to the first character of each match. Apparently, my understanding of pointers is lacking somewhere...

Here's what my StrStr function looks like right now:

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
unsigned int StrStr(const char* str, const char* target, char** retStrings){

    int strlength = 0, targetlength = 0, i = 0, counter = 0;
    char* tempptr = '\0';

    strlength = strlen(str);
    targetlength = strlen(target);

    cout<<strlength<<endl<<targetlength<<endl;

    for(i = 0; i < strlength; i++){
        if(strncmp(str+i,target,targetlength)==0){    //a match was found
            tempptr = (char*)str+i;
            retStrings[counter] = tempptr;
            counter++;
        }
    }

    return counter;
}

int main()
{
    const char teststring[] = "This is a test string of test words and other tests\0";
    const char testtarget[] = "test\0";
    char** stringtable;

    int i;

    cout<<"Attempting custom StrStr..."<<endl;
    cout<<StrStr(teststring,testtarget,stringtable);

    cout<<"Attempting built-in strstr..."<<endl;
    cout<<strstr(teststring,testtarget);

    cin >> i;

    return 0;
}


As it is now, it crashes in the if statement. Can any fresher eyes see what I'm doing wrong?
stringtable points to some random place in memory. You treat it as though it does not.

1
2
const unsigned reasonablyLargeValue = 256 ;
 char* stringtable[reasonablyLargeValue] ; 
Ah, I see. I tried initializing stringtable a few ways, but never exactly like that. Anyway, that did it. Thanks for your help!
Topic archived. No new replies allowed.