我只是試圖從字符串中提取兩個子字符串。但它顯示出分段錯誤。字符串中的段錯誤
的代碼是:
const char *str;
char *s1, *s2;
str = "name:d";
char *pos = strchr(str, ':');
size_t no = 1,
index = pos - str;
if (index > 0)
{
strncpy(s1, str, index);
cout << "name is:" << s1;
index++;
strncpy(s2, str + index, no);
cout << "direction is:" << s2;
}
屬於http://debug-my-code-for-me.com – 2013-03-14 11:47:38
C或C++?選一個。如果後者使用'std :: string'。 – 2013-03-14 11:48:04
@ user2114865如果指針不指向任何地方,它們會很傷心 – 2013-03-14 11:49:58