String from pointer to a char?
-
This is gonna sound like a really stupid question, but I'm new to C++ (been using C#). If I have a pointer to a char, how can I display the char in a messagebox? (I know to use MessageBoxA but I'm not sure how to make the char into something the MessageBoxA function accepts)
-
This is gonna sound like a really stupid question, but I'm new to C++ (been using C#). If I have a pointer to a char, how can I display the char in a messagebox? (I know to use MessageBoxA but I'm not sure how to make the char into something the MessageBoxA function accepts)
Omnicoder wrote:
If I have a pointer to a char...
Which looks like:
char c;
char *p = &c;Omnicoder wrote:
how can I display the char in a messagebox?
You'll need a pointer to an "array" of characters, like:
char *p = "Hello World";
MessageBox(p);MessageBox() expects a pointer to a null-terminated string which is different than a pointer to a character.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
This is gonna sound like a really stupid question, but I'm new to C++ (been using C#). If I have a pointer to a char, how can I display the char in a messagebox? (I know to use MessageBoxA but I'm not sure how to make the char into something the MessageBoxA function accepts)
In addition to David's answer, I want to say that you should never call a specific version of a function like MessageBox (so, calling MEssageBoxA or MessageBoxW is wrong). You should always stick to the generic version (without the A or W at the end). Character encoding causes in general a lot of troubles to new programmers, so I strongly suggest this excellent article[^] in order to better understand the concepts. Even if your code compiles now, reading this article can save you a LOT of time for the future (and probably a lot of problems too :-) ).
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++