-
Hi,
I've read through some examples that show how to concatenate strings but can't seem to get any of them to work in a Win32 app.
What I would like to do is the following:
char* val1;
char* val2;
MessageBox(hDlg,val1 + " " + val2,"Info",MB_OK);
Now I know that wont work as it is but I just wanted to make it clear what I wanted to do..
I'm not using MFC.
Any help and examples would be greatly appreciated..
Dan
-
You could use the
lstrcat api:
Code:
MessageBox(hDlg,lstrcat(val1, val2),"Info",MB_OK);
But if you include string you could also concatenate using the + you provided.
-
Code:
#include <string.h>
.
.
.
char buffer1[] = "Hello";
char buffer2[] = "You";
strcat(buffer1, ", ");
strcat(buffer1, buffer2);
cout<<buffer1<<endl;
-
Code:
#include <string>
using std::string;
...
char *one = "Hello";
char *two = "World";
MessageBox(NULL, string(one) + " " + two, "Message", MB_OK);
-
He already uses the MessageBox API call, so he probably already has
#include windows.h
so why not use the
lstrcat from the windows.h?
Saves you one header file ;)
-
You have to be careful not to get inconsistencies between the way all the arrays are allocated. For example, there are 3 ways just off the top of my head:
new / delete / delete[] (c++)
malloc / free (c)
GlobalAlloc / GlobalFree (windows)
The strcat functions automatically extend the char* array - so if it uses a different allocation method to the one you originally used to make the string, then you'll get a GPF :(
-
Thanks guys! ChimpFace9000's code works but I can't seem to get Jop's code to work.. On Jop's, it doesn't error out but it just doesn't show the MessageBox when I put the lstrcat(val1,val2) in there.. Oh well.. I would've liked to use that cause it's a lot cleaner..