Got error trying to returning a pointer to an array of String

Hi, My intention for the following function is that you input a string in and the function will split the string (substring it) into several strings seperated by ";". (Use Tokenizer to take care of that) Then, put these substrings into an array and return it.

I tried to read through the pointer tutorials several times, but I couldn't figure out what went wrong here in my code. It seems that when the program hit the returning part, the program just got error and I got the "myProgram.exe has encountered a problem and needs to close. We are sorry for the inconvenience." message popped up.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
wxString* AutoTokenize(const wxString* inString){
    int totalSC = 0;
    for(unsigned int i = 0l; i < inString->Length(); i++){
        if (inString->GetChar(i) == ';'){
            totalSC++;
        }
    }

    wxString* wxs = new wxString[totalSC];
    int i = 0;
    wxStringTokenizer tkz(inString->Mid(0,wxSTRING_MAXLEN), wxT(";"));
    while(tkz.HasMoreTokens()){
        wxString token = tkz.GetNextToken();
        wxs[i++] = token;
    }

    return wxs;
}


I am very confident that all the wxWidgets part (ex. wxString, etc.) are correct. I think that I am misunderstanding some concepts in term of using pointers correctly.

Please correct and advise me.

Thank you in advance.
A pointer points to a memorylocation. In this case, that memorylocation is bounded to a variable inside the scope of AutoTokenize(). Trying to access it from outside that scope, causes problems.

You could use another parameter, holding a pointer to the memory where you're going to store data in (I have never used this construction myself, but I think it should work).

Hope this helps
Last edited on
Actually, the problem is that I got Logic error. It's OK, problem solved.
Topic archived. No new replies allowed.