我只是试图从字符串中提取两个子字符串。但它显示出分段错误。字符串中的段错误
的代码是:
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