arrays - Splitting function in C++ -


i trying write function takes string , delimiter input , return array of strings. reason, following code runs segmentation error. wondering problem?

char** split(string thing, char delimiter){      thing+='\0';//add null signal end of string     char**split_string = new char*[100];      int i=0,j=0,l=0; //indexes- ith letter in string                     // j jth letter in lth string of new array     int length = thing.length();     while (i < length){             if ((thing[i]!=delimiter && thing[i]!='\0')){                     split_string[l][j]=thing[i];                     j++;             }             else {                     j=0; //reset j-value                     l++;             }             i++;     }      return split_string; 

}

after doing char**split_string = new char*[100];

you still need initialize each of 100 char * pointers created.

static const size_t str_len = 50; //assuming length not exceed  for( size_t ix = 0 ; ix < 100 ; ++ix){     split_string[ix] = new char[str_len]; } 

also need make sure while writing split_string not exceed allocated memory in case 50 , don't have splited strings more 100.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -