473,485 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem returning std::string from a function - always returns null

1 New Member
I am using the following recursive function for an assigment
Expand|Select|Wrap|Line Numbers
  1. string GetString(char numbers[100], int curNum){
  2.     string returnString = "";
  3.     int temp;
  4.     if (numbers[curNum] != '\0'){
  5.         for (int i = 0; i <=2; i++){
  6.             //temp = ;
  7.             returnString = returnString + keys[(int)numbers[curNum]-48][i];// + GetString(numbers, curNum + 1);
  8.         }
  9.     }
  10.     return returnString;
  11. }
The function is working correctly as I step through it and when I get to the end with the debugger on the like return returnString, the value is correct. But as soon as this line completes, returnString = null, and the function returns a null. I assume that this has something to do with the string variable being declared locally and is somehow an internal pointer that gets deleted... How can I get around this?
Sep 28 '07 #1
2 4574
Ganon11
3,652 Recognized Expert Specialist
Declare it using a pointer?
Sep 28 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
No it's not about using a pointer.

The return will call the std::string copy constructor so that's not an issue.

I suspect your debugging is at fault. Do this:

Expand|Select|Wrap|Line Numbers
  1. string GetString(char numbers[100], int curNum){
  2.     string returnString = "";
  3.     returnstring = "weaknessforats";
  4.      return returnString;
  5. }
  6.  
verify you get the string returned.

If you do, then you can search for the real culprit.
Sep 28 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1665
by: Paul Kirby | last post by:
Hello All I am trying to create a function that returns a string like the following: // defined in headerfile // void test_func(char *retstr); // -------------------- // // c/cpp File //
1
4377
by: Tero Toivanen | last post by:
Dear experts, I am doing code to Solaris 9 system with C++. I get every now and then segmentation fault in the following code that removes heading and trailing white spaces (mLineStr is of...
4
4230
by: __PPS__ | last post by:
Hello, I have a function that does some decoding (same as urldecode in php) and it returns std::string std::string urldecode(const char* in, std::size_t length); so, this function parses input...
5
2260
by: Alfonso Morra | last post by:
Hi, What is the recomended way of returning an STL container (e.g. std::string, std::vector etc fom a function? Is it by simply returning a local variable? (I doubt it) std::string...
3
5291
by: ezra epstein | last post by:
I'm been banging my head over this for a little while now. Here's a simple function to return a record: <code> CREATE OR REPLACE FUNCTION create_item_record_for_override(INTEGER, VARCHAR )...
7
11543
by: JustSomeGuy | last post by:
I need to make a class called uid. A UID is a unique identifier. It looks like... 1.2.3.345.1.2.4.566 This uid get transmitted over a network as 8 bit binary data. If the length of the UID is...
6
10918
by: Siam | last post by:
Hi all... What's the best way to return some string from a function (without using the string class). Would the following work: char* getString( ) { return "TheString"; }
3
3193
by: doubts | last post by:
Hi all, I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net. when using std::string type variable, the application causes exception at one instance and does not cause an exception at...
10
7283
by: mr_sorcerer | last post by:
Hi! I just found something interesting. I mean what do you think about this: char *p = 0; std::string str = p; Why std::string doesn't check null pointers?
5
4527
by: Ramesh | last post by:
Hi. Assuming I have a code snippet like below: #include <iostream> using namespace std; char Mac = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 }; std::string csMac;
0
7116
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6825
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7275
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5418
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4857
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4551
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1376
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.