嗨,我正在做类似于下面的示例的字符串标记化。但是,例如,在 while 循环中,我会将字母“a”更改为“hellow”。在分配给 myVar[i] 之前尝试更改 pch 时出现分段错误。我该怎么做呢?
map <int, char*> myVar;
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
int i = 0;
while (pch != NULL)
{
printf ("%s\n",pch);
//modify token value
stringstream strStream;
strStream << "hello_world";
char newStr[7] = {0};
memcpy(newStr, strStream, 7);
myVar[i] = (char*)newStr;
pch = strtok (NULL, " ,.-");
i++;
}