Combine 2 char arrays

Started by
2 comments, last by gretty 14 years, 2 months ago
Hi I am trying to figure out how to combine 2 char arrays. I know I could easily combine 2 strings but I'm pretty sure I cant use strings for what I am trying to do. I am trying to convert 2 integers(x,y) to characters(char arrays) so I can display them in a static control(SetWindowText()). I can convert each x,y value to a char array but I dont know how to combine the 2 char arrays to send them to the static control to display psuedo code/function:


void displayPosition(HWND hwnd, UINT msg, POINT focusPnt)
{
  // Post: Displays the x,y coordinate where the mouse is in static control

  char x[MAX_PATH];
  char y[MAX_PATH];
  char pnt[MAX_PATH];
  itoa(focusPnt.x,x,10);
  itoa(focusPnt.y,y,10);

  // how can I combine x & y so I can display the result in the static control
  
  SendDlgItemMessage(hwnd,msg,WM_SETTEXT,0,(LPARAM)pnt);  // set control text
}

Advertisement
Quote:Original post by gretty
Hi

I am trying to figure out how to combine 2 char arrays. I know I could easily combine 2 strings but I'm pretty sure I cant use strings for what I am trying to do.

I am trying to convert 2 integers(x,y) to characters(char arrays) so I can display them in a static control(SetWindowText()). I can convert each x,y value to a char array but I dont know how to combine the 2 char arrays to send them to the static control to display

psuedo code/function:
*** Source Snippet Removed ***


Of course you can use strings for this [smile]
// don't forget#include <sstream>void displayPosition(HWND hwnd, UINT msg, POINT focusPnt){  // Post: Displays the x,y coordinate where the mouse is in static control  std::stringstream ss;  ss << focusPnt.x << ", " << focusPnt.y;  // ss now contains something like "4, 2" (no quotes of course)    // ss.str() gives us an std::string  // calling c_str() on that string gives us a null-terminated C-style string  SendDlgItemMessage(hwnd,msg,WM_SETTEXT,0,(LPARAM)ss.str().c_str());  // set control text}

You just have to make sure SendDlgItemMessage() expects a null-terminated C-style string.
(Assuming C)

sprintf
void displayPosition(HWND hwnd, UINT msg, POINT focusPnt){  // Post: Displays the x,y coordinate where the mouse is in static control  char buf[MAX_PATH];  sprintf( buf, "(%i, %i)", focusPnt.x, focusPnt.y );    SendDlgItemMessage(hwnd,msg,WM_SETTEXT,0,(LPARAM)buf);  // set control text}
Thanks everyone for the help, I think I go with sprintf
Thanks again :)

This topic is closed to new replies.

Advertisement