Hi Everyone,
I am coding StrTok() which functioned as strtok predefine in cpp library. When it called this function second time and I got Access violation Error. Please see this piece of code.
When I debugged it and it showed this "char* CurToken = new char();" gave me Access Violation Error. I don't see anything wrong with it.Code:char* StrTok1(char* strToken, const char* strDelimit) { static char* strTokenCopy; static char* CurDelimitPos; char* CurToken = new char();//reset it every time char* ReturnCurToken = CurToken;//always points to CurToken if(strToken != NULL) { strTokenCopy = new char; //copy all elements for(char* strTokenPtr = strTokenCopy ; *strToken; *strToken++) { *strTokenPtr++ = *strToken; } *strTokenPtr = NULL; CurDelimitPos = strTokenCopy;//set CurDelimitPos points to first char of strTokenCopy } else { //advance to next position in stkTokenCopy *CurDelimitPos++; } for(char* Token = CurDelimitPos; *Token; *Token++) { const char* Delimit; //check if Token matchs any Delimit for(Delimit = strDelimit; *Delimit;)//Delimit looks in strDelimit { if(*Token != *Delimit) *Delimit++; else//found Token in strDelimit { *CurToken = NULL; CurDelimitPos = Token; break; } }//end for if(*Token == *Delimit) { break;//get out Token for loop } if(*Delimit == NULL) { *CurToken++ = *Token;//copy Token to CurToken } }//end for return ReturnCurToken; } void main(void) { char string[] = "A string\tof ,,tokens\nand some more tokens"; char seps[] = " ,\t\n"; char* token; cout<<"Tokens:\n"; token = StrTok1(string, seps); cout<<"\""<<token<<"\""<<endl; token = StrTok1( NULL, seps); cout<<"\""<<token<<"\""<<endl; token = StrTok1( NULL, seps); cout<<"\""<<token<<"\""<<endl; }
Thanks in advance.![]()



LinkBack URL
About LinkBacks



