Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to concat two char* in C++

First off, I'm a C# programmer, so my working knowledge of C++ is fairly limited. I took it back in college, but haven't touched it in 10 years, so please forgive me if this is relatively simple stuff.

I'm attempting to make a DLL that I can use in C# that implements the libwpd library.

I've managed to create a DLL that exports 2 functions that I can access via P/Invoke. The first returns a constant integer (generated by visual studio as a sample), the 2nd a string.

If I return a constant string from the function, it passes successfully to C# and I can read it on the other end, so I know the data is being passed back.

The problem I'm running into is with libwpd. I've had to modify their TextDocumentGenerator.cpp file to add the information to a char* instead of using the printf that they use so I can access it later.

I've added a variable definition to the public section of the header file so I can read it from the calling code.

Now, I'm trying to write a function that allows me to add the char* given by libwpd to the external char*.

I've come up with this:

char* addString(const char* addThis, char* toThis)
{
char* copier  = (char*)malloc(strlen(toThis) + 1 + 1);
strcpy(copier, toThis);
strcpy(copier, "1");

toThis = (char*)malloc(strlen(copier) + 1);
strcpy(toThis, copier);

return copier;
} 

But when I pass the information back, I get a blank string.

I call the function by calling totalFile = addString("\n", totalFile);

(I realize it should only technically add "1" to the string repeatedly, but it's not doing even that)

If i change the strcpy to strcat for the copier lines, it locks up.

I don't know how to create a program in C++ so I can even step through the functions to see what's happening.

Any assistance would be appreciated.

like image 710
Trevor Watson Avatar asked Feb 03 '26 12:02

Trevor Watson


2 Answers

Are you aware of existence of std::string? It's a class that handles strings in C++; char * is legacy from C.

std::string provides + operator, that does what you want.

like image 141
Griwes Avatar answered Feb 06 '26 00:02

Griwes


You need to allocate enough space for the return string, copy the inital string into the destination buffer, and then call strcat to append the extra info.

For example:

char* addString(const char* addThis, const char* toThis)
{
    char* destination = (char*)malloc( strlen( addThis ) + strlen( toThis ) + 1 );
    strcpy( destination, toThis );
    strcat( destination, addThis );
    return destination;
} 

Don't forget you'll need to call free( destination ) at some point after calling this function.

EDIT: Of course, this is just me fixing the function proposed in your question. Really, it's not a great idea to return a pointer from a function and count on the caller to free it. Since you're using C++ rather than C, you'd likely be much better off using some C++ constructs, such as std::string or at the very least, wrapping the char* in a shared_ptr or similar.

If you're going to be writing C++, I'd recommend buying yourself a good book on the subject, it really is far too easy to shoot yourself in the foot if you don't know what you're doing.

like image 31
obmarg Avatar answered Feb 06 '26 01:02

obmarg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!