Use std::string
, rather than C strings. Use string streams, rather than trying to concatenate non-string values to strings:
std::ostringstream oss;
oss << "twittermood.php?status=sendTweet&setting1=" << setting1;
use(oss.str()); // or use(oss.str().c_str());
If that API really needs a non-const
string (given that it doesn't even take the length of the string, I suppose it's just a buggy API disregarding const
), copy the string to a buffer and pass that:
const std::string& str = oss.str();
std::vector<char> buffer(str.begin(), str.end());
buffer.push_back('\0');
GETrequest(addr, port, &buffer[0], c);
As for what really happens when you do what you do:
"twittermood.php?status=sendTweet&setting1="
is an rvalue of the type char[43]
, which implicitly converts to const char*
, a pointer to the first character. To that you add an integer, by this forming a new pointer of the type const char*
pointing to some more or less random memory location. I suppose you try to pass this as the char*
to your API function, for which the const
would have to be dropped.
A C++ compiler, however, will never implicitly drop a const
— for your own good.