Using Char with multiple strings and constants

Hi all,

Im starting to get the hang of C++ (finally understood pointers :D) but one thing I just cannot work out is Char arrays.

Say I want to create a array that does the following

1
2
3
4
5
6
7
8
9
#define SIG roflmao
#define PORT 1234
"<hr />"
int version

char buffer[] = "<hr />"+SIG+":"+PORT+" "+version;
// in this form



How would I go about mashing all those variables into one array?

I know this is possible by using the string library, but I would like to learn how to do this using pure char arrays.

Thanks!
Or you can use sstream:

1
2
3
4
5
6
#include<iostream>
#include<sstream>

std::stringstream itos;
itos << "<hr />" << SIG << ":" << PORT << " " << version;
char buffer[] = itos.str();
That won't work, because str() returns a std::string.
Would that mean in the second example buffer would have to be string buffer?
Last edited on
No, you need a single string:
std::string str = itos.str();

But then you're back at using strings, which you wanted to avoid, although it is the correct thing to do.
Is the string library encouraged, or are we encouraged to use standard char arrays?

If so, what are the pros/cons of each var type?
Using std::string is encouraged. std::string automatically allocates and deallocates storage as needed, can hold strings of arbitrary length, has overloaded operators = and +/+= (for concatenation) and provides a number of other member functions. See here:
http://www.cplusplus.com/reference/string/string/

char arrays have no advantages worth speaking of over std::string.
Last edited on
Thanks for clearing that up! I'll look into it now :)
Athar: sorry typo, my bad.

I mean:
1
2
3
std::stringstream itos;
itos << "<hr />" << SIG << ":" << PORT << " " << version;
char buffer[] = itos.str().c_str();


Bud as you where saying char* as a string is a C way of doing it.
In C++ there is no reason not to use strings.

There are some pros to char* but not in a modern computers.
They use less memory and are generally faster is use.
Bud unless your programming microchips or you can't count the age of you computer on your hands and feet there's no reason to use them.
Last edited on
str() returns a temporary string and the pointer c_str() returns is no longer valid when the actual string is destroyed.
So that wouldn't work either.
std::string doesn't necessarily take more memory. While it's true that it usually allocates more than strictly necessary to account for future growth, there's an unholy tendency in C to create char arrays that are as long as the maximum length that the string is ever expected to assume. That doesn't exactly save memory.
Last edited on
itos.str().c_str() does work if you copy the string to a temporary container in the same line.
temporary string will be passed to c_str() and return a const char* to the location still containing the string.

But now that you mention it, it's not 100%.

And people that do char string[MAX_INT] or any of does funny thing I see at the university all the time...

/Slap for being lazy
/Slap for not reading your book

and

/Slap for me having to fix it EVERY time... xD
I'm not quite sure what you mean, but...
return a const char* to the location still containing the string.

Yes, at that point it still does. However, once the assignment is complete, the temporary string will be destroyed and the location no longer contains anything, i.e. buffer won't point to anything valid (assuming you meant const char* buffer=...).

Last edited on
I mean that is works as long as you know the memory location will not be overwritten. If you know this you can use realloc to make sure it's allocated, but come to think of it, then you need the number of chars too. And if you are going there you can just as well do:
1
2
string temp = itos.str();
const char* buffer = temp.c_str();


I other words, nvm me. I'll correct myself by saying, don't use is, use strings. :)
Topic archived. No new replies allowed.