Why can't I add these strings together?

I have these two `const char *` variables:

1
2
const char *ip = v.GetConstStrPtr();
const char *port = &p.GetConstStrPtr()[6];


Here the IP is `127.0.0.1`, and the port is `50001`.

When I try a simple string(ip) + string(":") + string(port), I get `127.0.0.1:0`, which is of cause wrong.
If I remove the string(ip) +, I get `:50001`, so this definitely means that the two variables are both valid.

I have also tried this with the same result:

1
2
std::ostringstream connect;
connect << "connect " << ip << ":" << port;


Why isn't this working, and how can I get this to work properly?
string(ip) + string(":") + string(port)

This is correct and should work just fine.

Is GetConstStrPtr returning a static buffer or something? Maybe calling it the second time is overwriting the original string data.

Can you post the contents of that function? I'm highly suspicious of anything that returns a pointer, because what is it pointing to?
Last edited on
&p.GetConstStrPtr()[6] refers to a vector and is retrieving the data from the sixth place in the array.
I printed the result out and I'm getting a `vector<t> too long` error, which doesn't make sense since I'm trying to pull data out of the array.

I ended up using this code to get the port without using the array vector type:

1
2
3
4
SFlashVarValue p("");
m_player->GetVariable([private location- not showing this!], &p);
string buffer = &p.GetConstStrPtr()[6];
string portstr = buffer.substr(buffer.length() - 5);


I printed `portstr` out, and the result was the `50001` I'm looking for, but however the final output is still 0 when added to the ip.

Both are the same types, are declared in the same way (the IP doesn't use the string buffer), and both are valid.
This can't be to do with the way GetVariable is called as the final 'port' value is assigned to a new variable type which has no connection whatsoever to the GetVariable function.

This is to do with simple string concats, but I've tried my methods on http://coliru.stacked-crooked.com/ and they're working.
This confuses me.
&p.GetConstStrPtr()[6] refers to a vector and is retrieving the data from the sixth place in the array.


So 'p' and 'v' objects each have their own vector? There's no data overlap here?

I printed the result out and I'm getting a `vector<t> too long` error, which doesn't make sense since I'm trying to pull data out of the array.


What is the exact error message (don't summarize) and exactly which line of code does the error occur on?

I printed `portstr` out, and the result was the `50001` I'm looking for, but however the final output is still 0 when added to the ip.


You are missing something. Nothing you posted here has the problem. The problem either is in one of the functions you are calling, or is in how you are printing the data (maybe you are truncating the output somehow?).

I'm afraid I won't really be able to help further without seeing more code.

What I can say for certain is that the problem is not with string concatenation. At least not if you are doing it how you illustrated in this thread. It works how you expect it would.
Topic archived. No new replies allowed.