how to properly output TCHAR from function?
문제
This code:
#include <tchar.h>
TCHAR example_function() {
TCHAR example_tchar[10];
return example_tchar;
}
int main() { }
Gives error:
In function 'TCHAR example_function()':
error: invalid conversion from 'TCHAR* {aka char*}' to 'TCHAR {aka char}' [-fpermissive]
warning: address of local variable 'example_tchar' returned [enabled by default]
해결책
Your variable example_tchar
is not a TCHAR
, but an array.
What are you actually trying to achieve?
If you want to return a single TCHAR
:
TCHAR example_function() {
TCHAR example_tchar[10];
return example_tchar[0];
}
//or simply
TCHAR example_function() {
TCHAR example_tchar = _T('');
return example_tchar;
}
If you want to return an array, or rather a pointer, you'll have to dynamically allocate memory to prevent undefined behavior:
TCHAR* example_function() {
TCHAR* example_tchar = new TCHAR[10];
return example_tchar;
}
다른 팁
And if you want to return a string of TCHARs
use something more complex, like std::basic_string<TCHAR>
for example (or use pointers, or just add set TCHAR[10]
as return parameter). TCHAR
is just a define that selects between char and wchar_t based on build options.
제휴하지 않습니다 StackOverflow