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