Click here to Skip to main content
15,896,111 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Passing a String as function Argument Pin
Cedric Moonen6-Jul-10 3:47
Cedric Moonen6-Jul-10 3:47 
GeneralRe: Passing a String as function Argument Pin
T.RATHA KRISHNAN6-Jul-10 3:54
T.RATHA KRISHNAN6-Jul-10 3:54 
GeneralRe: Passing a String as function Argument Pin
Cedric Moonen6-Jul-10 4:14
Cedric Moonen6-Jul-10 4:14 
GeneralRe: Passing a String as function Argument Pin
T.RATHA KRISHNAN6-Jul-10 4:35
T.RATHA KRISHNAN6-Jul-10 4:35 
GeneralRe: Passing a String as function Argument Pin
Cedric Moonen6-Jul-10 4:49
Cedric Moonen6-Jul-10 4:49 
QuestionRe: Passing a String as function Argument Pin
David Crow6-Jul-10 4:19
David Crow6-Jul-10 4:19 
AnswerRe: Passing a String as function Argument Pin
KarstenK6-Jul-10 4:23
mveKarstenK6-Jul-10 4:23 
AnswerRe: Passing a String as function Argument Pin
Aescleal6-Jul-10 4:40
Aescleal6-Jul-10 4:40 
Don't use printf. The interface to printf isn't type checked so the compiler has no idea that you're passing complete rubbish to it. What's happening is...

- your code sticks a pointer to a lump of wide characters on the stack
- printf looks at it's format string and sees that you want the first argument on the stack interpreted as a lump of ordinary characters terminated by a zero
- it starts reading your wide character array, sees the first character is non-zero but, as it's reading the memory byte by byte it sees the high byte of the first character as the string terminator. It prints one character
- printf stops processing having done everything you told it to do

So what can you do about it?

The first thing is stop using printf. Printf is a C function - while it's bundled in the C++ library it's pretty useless. Use streams, they're the equivalent C++ mechanism. AND you don't have to convert your function arguments either. Instead of trying to use:

printf( "%s\n", str.c_str() );


you can use:

std::wcout << str << std::endl;


Look ma, no conversions! Same number of function calls and the compiler can probably optimise it a lot easier. You also get type safety - if you try and use a stream with an object type it doesn't understand the compiler will tell you.

Cheers,

Ash
GeneralRe: Passing a String as function Argument Pin
Niklas L6-Jul-10 21:38
Niklas L6-Jul-10 21:38 
GeneralRe: Passing a String as function Argument Pin
Aescleal6-Jul-10 21:54
Aescleal6-Jul-10 21:54 
GeneralRe: Passing a String as function Argument Pin
Niklas L6-Jul-10 22:00
Niklas L6-Jul-10 22:00 
GeneralRe: Passing a String as function Argument Pin
Aescleal7-Jul-10 5:49
Aescleal7-Jul-10 5:49 
QuestionMFC Smart Device Article Pin
D.Manivelan6-Jul-10 1:54
D.Manivelan6-Jul-10 1:54 
AnswerRe: MFC Smart Device Article Pin
Richard MacCutchan6-Jul-10 3:22
mveRichard MacCutchan6-Jul-10 3:22 
QuestionMask Pin
john56326-Jul-10 1:15
john56326-Jul-10 1:15 
AnswerRe: Mask Pin
Niklas L6-Jul-10 1:27
Niklas L6-Jul-10 1:27 
QuestionMoving from VS2008 to VS2010, now 'intrin.h' fails to compile Pin
eight5-Jul-10 22:50
eight5-Jul-10 22:50 
AnswerRe: Moving from VS2008 to VS2010, now 'intrin.h' fails to compile Pin
«_Superman_»5-Jul-10 22:58
professional«_Superman_»5-Jul-10 22:58 
GeneralRe: Moving from VS2008 to VS2010, now 'intrin.h' fails to compile Pin
eight5-Jul-10 23:04
eight5-Jul-10 23:04 
AnswerRe: Moving from VS2008 to VS2010, now 'intrin.h' fails to compile Pin
KarstenK6-Jul-10 4:24
mveKarstenK6-Jul-10 4:24 
QuestionERROR: Cannot open include file: 'hidsdi.h': No such file or directory Pin
learningvisualc5-Jul-10 21:48
learningvisualc5-Jul-10 21:48 
AnswerRe: ERROR: Cannot open include file: 'hidsdi.h': No such file or directory Pin
«_Superman_»5-Jul-10 21:53
professional«_Superman_»5-Jul-10 21:53 
AnswerRe: ERROR: Cannot open include file: 'hidsdi.h': No such file or directory Pin
CPallini5-Jul-10 21:56
mveCPallini5-Jul-10 21:56 
GeneralRe: ERROR: Cannot open include file: 'hidsdi.h': No such file or directory Pin
learningvisualc5-Jul-10 22:08
learningvisualc5-Jul-10 22:08 
GeneralRe: ERROR: Cannot open include file: 'hidsdi.h': No such file or directory Pin
CPallini5-Jul-10 22:16
mveCPallini5-Jul-10 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.