因此,我正在尝试实现一个不使用任何 C 库函数(如 strtok() 等)的令牌解析器,但我遇到了一些访问冲突问题,并且在阅读了几个类似的问题后仍然没有得到它下。有人愿意提供一些指点吗?
int main(int argc, char* argv[])
{
int maxTokens = 10;
char* tokens[10];
int i;
for(i = 0; i < maxTokens; i++)
{
tokens[i] = NULL;
}
char* str = "This,is,a,test,string";
int result = parseLine(str, ',', tokens, maxTokens);
printf("%d tokens were found!", result);
system("PAUSE");
return 0;
}
int parseLine(char* str, char delimeter, char* tokens[], int maxTokens)
{
char* srcStr = str;
int strlen = 0;
int tokenCount = 0;
if(srcStr[strlen] != delimeter && srcStr[strlen] != '\0')
{
tokens[tokenCount] = (char*) malloc(sizeof(char)*strlen+1);
tokens[tokenCount] = &srcStr[strlen];
tokenCount++;
}
while(srcStr[strlen] != '\0')
{
if(srcStr[strlen] == delimeter)
{
tokens[tokenCount-1][strlen] = '\0';
if(srcStr[strlen+1] != '\0')
{
tokens[tokenCount] = (char*) malloc(sizeof(char)*strlen+1);
tokens[tokenCount] = &srcStr[++strlen];
tokenCount++;
}
}
else
{
strlen++;
}
}
return tokenCount;
}