#include <iostream>

#include<string>



using namespace std;

class substr

{

      public:

      void getsubstr(char* b, char* db)

      {

      char* stpos = strstr(db,"gi|");

      int pos = db-stpos; 

      char* end = strstr(db,"|ref");

      int endpos = abs(db-end);

      b= new char[endpos-pos+2];

      int i=pos+3;

         for(;i<endpos;i++)

          {

           *b++=*(db+i); 

          }

        *(b+i)='\0';



       }

 };

      int main()

      {
       substr* s;
       char* b;
       char* db= "gi|71772593|ref|NM_001032.3|"; 

          s->getsubstr(b,db);
          cout<<b<<endl;

          delete s;



      }

/*Error Message
)t$1EDE
                              D$$9uރ
                                                     [^_]Ë$ÐUSt1Ћu[]ÐUS
*** glibc detected *** ./a.out: free(): invalid pointer: 0xb7f81dc0 ***
*/

Edited 3 Years Ago by mike_2000_17: Fixed formatting

1. Use code tag to present your snippets:
[code=cplusplus] your code

[/code]
2. Stop this terrible source alignment! Use indentation.
3. You declare uninitialized pointer to substr class object (s). But where is the object?
4. Your substr class contains the only (and very dangerous) member function. It's a very bad design solution (looks like OOP caricature). Avoid using substr name for your classes: it's a well-known member function of std::string C++ library class.

I would suggest you change the increment of *b++ to *(b+(j++))= *(db+i) ... and initialize j with 0;before the loop and also remove the
delete s;
as you have not assigned any memory to it.
Plus you should also return b from your function getsubstr()

I would suggest you change the increment of *b++ to *(b+(j++))= *(db+i) ... and initialize j with 0;before the loop and also remove the
delete s;
as you have not assigned any memory to it

ArkM's idea of using std::strings is a much better unless the OP is doing this for educational purposes.

This article has been dead for over six months. Start a new discussion instead.