1

I have a problem that how can I return a string between ranges(given by user) of char Array. Example:

  Entered string is “My name is john".

Start index: 3 Stop index: 6 Function will return “name”

my code is here but i will get address only as a output

#include <iostream>
#include <conio.h>
#include <string>
#include <cstring>
using namespace std;

string *section(char*ary, int index_1, int index_2)
{
    string sec=ary;
    string *str;
    str = &sec;
    *str = sec.substr(index_1, index_2);
    return str;
}


int main()
{
    int starting_index = 0;
    int ending_index = 0;

    char *ptr;
    ptr = new char[200];
    int i = 0;
    char ch = _getche();
    while (ch != 13)
    {

        ptr[i] = ch;
        i++;
        ch = _getche();
    }
    for (int j = 0; j < i; j++)
    {
        cout << ptr[j];
    }
    cout << endl;

    cout << "Enter start index: " << endl;
    cin >> starting_index;
    cout << "Enter end index: " << endl;
    cin >> ending_index;
    cout<<section(ptr, starting_index, ending_index);

   delete[] ptr;
  system("pause");
}
suleman
  • 38
  • 9
  • 2
    You return a pointer to a local variable. *Don't* return a pointer, return a string *by value*. – Some programmer dude Jan 27 '16 at 08:57
  • Further, you can write `std::string(ary + index_1, index_2 - index_1 + 1)` to directly create the `string` - you don't need a `section` support function. – Tony Delroy Jan 27 '16 at 09:01
  • But, how can I get string from **section** function. – suleman Jan 27 '16 at 09:24
  • As Joachim said *"return a string by value"* - `string section(char* ary, int index_1, int index_2) { return `, as I said `std::string(ary + index_1, index_2 - index_1 + 1);`. Add a `}` and you're done. – Tony Delroy Jan 27 '16 at 10:09

1 Answers1

0

Your main problem is that you return a pointer. In other words, you return an address of a string object. This has two effects. Firstly, you pass the returned address to cout, instead of the pointed string. If your intention was to print the string, then you should have dereferenced the pointer. But there's the another problem. The returned pointer is invalid, because it pointed to a local object that was destroyed when the function ended. So you may not dereference the pointer. It's useless.

Both issues can be solved by returning a string by value from section, rather than an address.

please explain difference between these two prototype. string section( paramerters ) and string *section( paramerters )

The part on the left side of the function name (which is section) is type of the object that the function returns. The difference between string and string* types is that the latter is a pointer type. The value of a pointer is the memory address where the pointed object is located. So, the former function prototype declares a function that returns a string, while the latter declares a function that returns a pointer to string.

Few other tips besides the bug: The function fiddles pointlessly with pointers. The str variable is unnecessary. You never use the index arguments. In main you do unnecessary dynamic allocation.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • please explain difference between these two prototype. **string section( paramerters )** and **string *section( paramerters )** . – suleman Jan 27 '16 at 09:56
  • @jonny I updated my answer with that question. For more information, see http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – eerorika Jan 27 '16 at 10:04