Jump to content
  • Advertisement
Sign in to follow this  
Elspin

Concatenating pointers in C++?

This topic is 3993 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have 2 const char* pointers, and I need to concatenate them, but I can't just do this:
const char* concatenate = "lua/" + lua_tostring(luaVM, -1);

because they are pointers. So, how can I do so? I've been tried about everything I can come up with.

Share this post


Link to post
Share on other sites
Advertisement
If your intent is to represent strings, you should use std::string, which provides concatenation through '+'.

Otherwise, you can consider the strlen, malloc, strncpy, strncat, snprintf and free functions as means of concatenating C-style strings, though this will certainly amount to more work and require more learning than using std::string directly.

Share this post


Link to post
Share on other sites
If you have to concatenate two blocks of data, you must determine how much data the two blocks occupy, allocate sufficient memory, and copy the memory from both blocks into the newly allocated memory as appropriate.

If you're going to use C-style strings, this will be slightly messy. However, if you use C++ strings, the gruntwork is taken care of for you:

std::string a = "stringa";
std::string b = "stringb";
std::string c = a + b;

Share this post


Link to post
Share on other sites
I do not have the option of using these types. It must be with const char*, does anyone know how I can do it with this type?

Share this post


Link to post
Share on other sites
Unless this is a homework assignment were you're not allowed to use std::string, you *can* use it. Since it looks like you're interfacing with Lua (which I'm guessing is a C library), you can use the std::string constructor which takes a const char* and the c_ptr() method which returns a const char*. Those two make it easy to use C code while retaining the usefulness of std::string.

Share this post


Link to post
Share on other sites
Quote:
Original post by Elspin
I do not have the option of using these types.


If you do not know how to concatenate two strings represented as const char* together, you have no business working on such a project. It requires a level of skill beyond your current one.

Quote:
It must be with const char*.



#include <cstring>

const char *prefix = "lua/";
const char *suffix = lua_tostring(luaVM, -1);

const std::size_t psize = std::strlen(prefix);
const std::size_t ssize = std::strlen(suffix);

char *buffer = new char[psize + ssize + 1];

try
{
std::strncpy(buffer, prefix, psize+1);
std::strncat(buffer + psize, suffix, ssize+1);

// Use 'buffer' here

delete [] buffer;
}
catch(...)
{
delete [] buffer;
throw;
}


[Edited by - ToohrVyk on January 12, 2008 5:16:35 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
Unless this is a homework assignment were you're not allowed to use std::string, you *can* use it. Since it looks like you're interfacing with Lua (which I'm guessing is a C library), you can use the std::string constructor which takes a const char* and the c_ptr() method which returns a const char*. Those two make it easy to use C code while retaining the usefulness of std::string.


I have never seen the "c_ptr()" method referenced anywhere, is it's parameter simply the string which you want to convert to a const char*?

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk

#include <cstring>

const char *prefix = "lua/";
const char *suffix = lua_tostring(luaVM, -1);

const std::size_t psize = std::strlen(prefix);
const std::size_t ssize = std::strlen(suffix);

char *buffer = new char[psize + ssize + 1];

try
{
std::strncpy(buffer, prefix, psize);
std::strncat(buffer + psize, suffix, ssize);

// Use 'buffer' here

delete [] buffer;
}
catch(...)
{
delete [] buffer;
throw;
}


When I printed the buffer to the console, the two strings had some very strange characters between them.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by Elspin
I have never seen the "c_ptr()" method referenced anywhere, is it's parameter simply the string which you want to convert to a const char*?


He meant std::string::c_str().


Thanks. I'll use std::string to concatenate the pointers then reconvert them to const char* then I guess.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!