嗨即時做字符串標記化類似於下面的例子。然而,在while循環中,例如,我將改變字母'a'爲'hellow'。在分配myVar [i]之前嘗試更改pch時,出現分段錯誤。我該如何去做呢?strtok字符串和修改令牌值
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++;
}
你的意思是 「改變PCH」? – Gian
您發佈的代碼不是您遇到問題的代碼(對令牌沒有任何更改)。發佈*不*工作的代碼。 –
嘿抱歉,我已經編輯了上面的代碼。 – nuttynibbles