I need to know how (if even possible) to return a substring without having to return a local variable or space allocated by new

here's some code as an example as to what I'm looking for.

char* substr(const char* str, int start, int len)
{
   char retSub[len+1];
   int pos=start;
   while (pos-start<len)
   {
      retSub[pos-start]=str[pos];
   }
   retsub[pos]=0;
   return retsub;
}

We want to try and avoid the 'new' operator...

If I have to I will use the following:

void substr(const char* str, int start, int len, char*& puthere)
{
   //same code, exchange 'retval' with 'puthere' and return nothing
}

-----
This is for use in a dynamic string class I have been working on... this will be my ?6th? time... I have finally taken Ancient Dragon's advice and 'upgraded' from Dev-C++ to VC++
Thank you for this Ancient Dragon, it has been much help.
Hopefully this will be my last effort, so far I have 600 lines of code, almost each one commented, and I have thouroghly debugged each and every function, and the allocation process seems to be working out wonderfully!

Recommended Answers

All 3 Replies

On a slightly different subject... When should the keyword 'this' be used?

If any of this does not make sence, take a look at this, it is pretty well written explanation.

Pointer/pass by value

What you can do is in the variables you pass in, pass in a empty char*. certain data types are pass by value and some are pass by reference (but u can change this with a &). char* would be pass by reference. So if you do...

#include <iostream>

using namespace std;

void myFunction(char num, char* temp)
{
    temp[1] = num;
}

int main()
{

 char* temp = new char(2);
 temp[0] = 'h';
 temp[1] = 'i';

 cout << "\nBefore: " << temp[0] << temp[1];
 
//when we call this function, we are passing these variables by reference!
 myFunction('f',temp);
 
 cout << "\nAfter: " << temp[0]  << temp[1] << endl;
 return 0;
}

Output:
Before: hi
After: hf


so in short, pass by reference passes a pointer to where it can find the data.
pass by value passes a "copy" of the data.

just fyi...
pass by value:
int, single, double.

pass by reference
object, string, char*.


as for this...

#include <iostream>

using namespace std;


class myFunction{

        int num;

public:
        myFunction()
        {
                this->num = 3;
        };

        void mySuperFunction(int num)
        {
                cout << "num:> " << num << endl;
                cout << "this->num:> " << this->num << endl;
        };
};

int main()
{
        //create instance of myFunction and call constructor...
        myFunction myF;

        myF.mySuperFunction(45);

        return 0;
}

Output:
num:> 45
this->num:> 3

> char* temp = new char(2);
Don't you mean char* temp = new char[2]; Later, you'll need delete [] temp;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.