2

I was wondering if there is any advantage (for shorter strings only) of using a string datatype instead of a char array, or simply the string such as:

TextOut(hDC, 10, 10, "Hello", sizeof("HEllO") - 1)

2 Answers 2

1

In practice, this doesn't matter at all.

That said, the Win32 APIs require LPSTRs or LPWSTRs, so anything which is not "one of those" will first have to be converted into the appropriate char* type, so a very tiny bit of extra work is required.

I'd say the vastly bigger consideration is using a datatype which is convenient/familiar/easy for you to work with.

Sign up to request clarification or add additional context in comments.

Comments

1

One difference between a string datatype and the inline string as you've used them above is that using a separate named reference (whether the type is a string or char array) prevents the common bug where you change the string but forget to change the copy inside the sizeof(). If the new string is a different length, it will have undesired consequences. Having a single place that allows you to update both simultaneously, whether via a const char* or string datatype, is a better practice.

const TCHAR TEXTOUT_TEXT[] = _T("Hello");
TextOut( hDC, 10, 10, TEXTOUT_TEXT, sizeof(TEXTOUT_TEXT) / sizeof(TEXTOUT_TEXT[0]) - 1 );

1 Comment

@user9955048 Thanks man, but I was just asking about the difference between the three. Since I do a lot of assembly programming, I am most comfortable with using pointers for strings. So most of the times, I use something like: 'char *text="hello";' But thanks for pointing that out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.