访问数组指针时出现分段错误

时间:2019-06-28 20:13:57

标签: c++

我创建函数:

string* parts (string pattern, string input, int* len) {
  regex r(pattern);
  smatch m;
  if (regex_search(input, m, r) && m.size() > 0) {
    string first = m.str(0);
    int i = input.find(first);
    string a[3];
    a[0] = input.substr(0, i);
    a[1] = input.substr(i, first.length());
    a[2] = input.substr(i + first.length());
    *len = 3;
    return a;
  } else {
    string a[0];
    *len = 0;
    return a;
  }

}

但是当我尝试使用这样的功能时:

int len;
string *p;
p = parts("\\+", input, &len);
cout << "len: " << len << ".\n";
for (int i = 0; i < len; i++) {
    cout << "p[" << i << "]: ";
    cout << *(p + i) << ".\n";
}

控制台出现错误Segmentation fault(代码转储)

0 个答案:

没有答案