   [C++] Incorrect return value
  Hi all again , i was trying to make a function that will scan a string , and by setting the string for searching , the start text ,  the end text , will print all the data between start text & end text.Here's my function:char * scan_string(string buffer,string Start,string End){string::size_type pos=buffer.find(Start)+Start.length();string::size_type pos2=buffer.find(End)-pos; char str2[pos];const char *str =buffer.c_str();strncpy (str2,str,pos2);str2[pos+1]='\0';  return str2;}and a test run:    string do_test = "CODE_blabla_ENDCODE";    cout <<(scan_string(do_test,"CODE_","_ENDCODE"));The output should be "blabla" , but it shows only the first 5 characters("CODE_").Any ideas on how to fix it?   
  what youve done makes pos2 the length of the thing you want to copy and when you say strncpy(str2, str, pos2) it copies the right amount starting from the beginning not from pos (where you want to start copying from.   ;  string::size_type pos=buffer.find(Start)+Start.length();//this returns an index after the Start stringstring::size_type pos2=buffer.find(End)-pos;//this returns in index at the beginning of the End string MINUS the index after //the Start string!!  you really just want the index at the beginning of the End //string!string::size_type pos=buffer.find(Start)+Start.length();string::size_type pos2=buffer.find(End);char* str2 = new char[pos2-pos]; //we want the size of the sandwiched stringconst char *str =buffer.c_str();strncpy (str2,str+pos,pos2);str2[pos2-pos]='\0';return str2;   ;  Why are you all returning a char* from a function that takes two strings?string scan_string(const string& buffer, const string& Start,const string& End)  const string::size_type pos=buffer.find(Start)+Start.length();  const string::size_type pos2=buffer.find(End, pos); // start searching after the start in the unlucky event that Begin = "someString" + End  const string::size_type length = pos2-pos;  assert(length>=0); // check so that the length is valid  return buffer.substr(pos, length);}You probably also should check the find()'s for returning npos.hth   ;  " char str2[pos];" isn't even valid C++ (where 'pos' is not a compile-time constant).Seriously, listen to sirGustav. std::string exists to make your life easier. Why would you spit in its face like this?   ;  Thanks all(={Anonymous poster,David55555,sirGustav,Zahlman};) , it works perfect!   