char* combination problem :(

X

x

Im trying to convert a few variables into a string object ( a
character string object). Only I have forgotten how! how
embarrasing....

char *x = (int1 + "/" + int2 + "/" + int3);

whats wrong? int1-3 can equal 0-99...




































6e
 
C

Christopher Benson-Manica

x said:
Im trying to convert a few variables into a string object ( a
character string object). Only I have forgotten how! how
embarrasing....
char *x = (int1 + "/" + int2 + "/" + int3);
whats wrong? int1-3 can equal 0-99...

Simple - you forgot what language you were using. + isn't a catch-all
string creation operator in C++ like it is in various other languages.
If you want a string, declare a string. A decent C++ book and the FAQ
(posted below) will be most helpful.

http://www.slack.net/~shiva/welcome.txt
http://www.parashift.com/c++-faq-lite/
 
R

Rolf Magnus

x said:
Im trying to convert a few variables into a string object ( a
character string object). Only I have forgotten how! how
embarrasing....

char *x = (int1 + "/" + int2 + "/" + int3);

whats wrong?

Doesn't make much sense to add integers and pointers together and write
the result into a pointer. Rembember: char* is not a string, but a
pointer to char. Try:

std::stringstream stream;
stream << int1 << "/" << int2 << "/" << int3;
std::string x = stream.str();
 
X

x

Rolf Magnus said:
Doesn't make much sense to add integers and pointers together and write
the result into a pointer. Rembember: char* is not a string, but a
pointer to char. Try:

std::stringstream stream;
stream << int1 << "/" << int2 << "/" << int3;
std::string x = stream.str();


the only problem with that is that the function I am tryin to use the
string of characters with requires an input of char*......
 
C

Christopher Benson-Manica

x said:
the only problem with that is that the function I am tryin to use the
string of characters with requires an input of char*......

Still easy, if const char* is acceptable - std::string's c_str()
method converts the string to an array of characters, C-style. If you
need a non-const char *, then you'll have to declare an array of
characters and populate it appropriately (I suggest sprintf):

char buf[256]; // larger than required, just make sure it's big enough
// as sprintf will happily overflow the buffer if it
// isn't

sprintf( buf, "%d/%d/%d", int1, int2, int3 );
 
B

Bill Seurer

x said:
the only problem with that is that the function I am tryin to use the
string of characters with requires an input of char*......

You can extract a char* from a string. See the functions in the string
class.
 
K

Kevin Goodsell

Christopher said:
the only problem with that is that the function I am tryin to use the
string of characters with requires an input of char*......


Still easy, if const char* is acceptable - std::string's c_str()
method converts the string to an array of characters, C-style. If you
need a non-const char *, then you'll have to declare an array of
characters and populate it appropriately (I suggest sprintf):

char buf[256]; // larger than required, just make sure it's big enough
// as sprintf will happily overflow the buffer if it
// isn't

sprintf( buf, "%d/%d/%d", int1, int2, int3 );

Or you could use Rolf's answer, and copy from the std::string into your
char array:

strcpy(buf, x.c_str()); // Be careful!!

Be sure to check the length first, and don't overflow buf. You could
even do this:

std::vector<char> vec_x(x.begin(), x.end());
vec_x.push_back('\0');

// function that takes (non-const) char*:
SomeFunc(&vec_x[0]);


It occurs to me that a streambuf that writes to a std::vector<CharType>
could be useful in these cases -- you could skip creating a std::string
then copying it into a vector, and write into the vector in the first place.

-Kevin
 
C

Christopher Benson-Manica

Kevin Goodsell said:
strcpy(buf, x.c_str()); // Be careful!!

Or better,

strncpy( buf, sizeof buf - 1, x.c_str() );

although the vector solution is probably preferable. Nice one, btw.
 
S

Sam Dennis

Christopher said:
Or better,

strncpy( buf, sizeof buf - 1, x.c_str() );

That's not such a great idea if one is to do C-style string processing
on the result; strncpy doesn't guarantee null-termination. (Also, the
arguments are in the wrong order.)

If you must do this sort of thing in C++, this should be safe:

char buf[N];
*buf = 0;
strncat( buf, x.c_str(), sizeof buf - 1 );
 
C

Christopher Benson-Manica

That's not such a great idea if one is to do C-style string processing
on the result; strncpy doesn't guarantee null-termination. (Also, the
arguments are in the wrong order.)

Yikes, I can't believe I blew it on the argument order. Well,
actually I can... *sigh*

As far as null-termination goes, you're right in general - but when
strncpy is used intelligently (as I attempted to do, but failed...),
it does null-terminate the string.
If you must do this sort of thing in C++, this should be safe:
char buf[N];
*buf = 0;
strncat( buf, x.c_str(), sizeof buf - 1 );

Agreed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top