473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Cannot properly return unsigned char*...

yabansu
14
Hi everyone,

I have a problem in returning an unsigned char* from a function.

In the main function:

string name = "yabansu";
unsigned char* arr1 = name.c_str();
cout << arr1 << endl; //Correctly prints "yabansu"

Let's say I wrote a function called StringToBytes for doing the same thing above as the following:

unsigned char* StringToBytes(string s)
{
cout << (unsigned char*) s.c_str() //Correctly prints "yabansu"
return (unsigned char*) s.c_str();
}

Then I call this function in the main() as below;

string name = "yabansu";
unsigned char* arr2 = StringToBytes(name);
cout << arr2 << endl; //prints weird characters instead of "yabansu"

As you see, nothing is different and the string("yabansu") can be properly displayed within the StringToBytes() function.

What could be the reason? Thanks...
Jan 31 '07 #1
1 4239
horace1
1,510 Expert 1GB
C and C++ pass actual parameters by value (unless they are C++ reference paramters), e.g.
Expand|Select|Wrap|Line Numbers
  1. unsigned char* StringToBytes(string s)
  2. {
  3. cout << (unsigned char*) s.c_str() << endl;//Correctly prints "yabansu"
  4. return (unsigned char*)s.c_str();
  5. }
  6.  
when you call StringToBytes()
Expand|Select|Wrap|Line Numbers
  1. unsigned char* arr2 = StringToBytes(name);
  2.  
a temporary copy is made of paramter name and this copy is passed into StringToBytes().
you can then call the c_str() function which returns a const pointer to the C-style version of the invoking string (the temporary copy).
then problem is when you return from StringToBytes() the temporary copy of the parameter is destructed and the pointer value returned as a function result is no longer valid, see
http://msdn2.microsoft.com/en-us/library/3372cxcy(VS.80).aspx

you can overcome this problem by passing name as a reference parameter (using &) e.g.
Expand|Select|Wrap|Line Numbers
  1. unsigned char* StringToBytes(string &s)
  2. {
  3. cout << (unsigned char*) s.c_str() << endl;//Correctly prints "yabansu"
  4. return (unsigned char*)s.c_str();
  5. }
a reference to name is passed and the pointer from s.c_str() is valid on return from the function as it points to a C-style version of name.

another thing to be careful of is c_str() returns const pointer (the character data pointer to should not be modified) and the cast
Expand|Select|Wrap|Line Numbers
  1. return (unsigned char*)s.c_str();
  2.  
removes the const so you could do something like
Expand|Select|Wrap|Line Numbers
  1. unsigned char* arr2 = StringToBytes(name);
  2. arr2[3]='c';
  3.  
the function result of StringToBytes() should be const qualified
Jan 31 '07 #2

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

Similar topics

7
by: Ramiro Barbosa, Jr. | last post by:
All, Any ideas why the following code prints zeros for the message id, number of packets and sequence number as shown in the sample output below? Somehow I am not being able to access the...
20
by: Brad Eck | last post by:
"The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function). Quoting from Stroustrup's 3rd edition of _The...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
12
by: Sheldon | last post by:
#define UWORD2 unsigned short int typedef BYTE Boolean; Boolean evaluateBit(UWORD2 *value, int bitnumber); Boolean evaluateBit(UWORD2 *value, int bitnumber) { int one=1; return (one &(*value...
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
5
by: wong_powah | last post by:
#include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
2
by: anson | last post by:
this code is an example code of TPOP, i type them and can be compiled... but when give some input , it getting segment fault .... i observed that when the build() initial the word table ... it...
4
by: ssylee | last post by:
I have set my year value in the register on my RTC (DS1302) to be 2008 using this code: // This function sets the year on the RTC // Note: The year takes in only the last two digits of the year...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...

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.