Unable to convert from 'const wchar_t *' to '_TCHAR *'

_TCHAR* strGroupName = NULL; const _TCHAR* strTempName = NULL; //Assign some value to strTempName strGroupName = _tcschr(strTempName, 92) //C2440 

I get an error in the above line when compiling this code in VS2008. In VC6, it compiles fine.

Error C2440: '=': cannot convert from 'const wchar_t *' to '_TCHAR *'

What is the problem and how to fix it?

+4
source share
4 answers

Try using it like

 strGroupName = (_TCHAR*)_tcschr(strTempName, 92); 

It seems to me that VS2008 got a slightly stricter type of type conversion and will not automatically do this in some cases.

+5
source
 strGroupName = const_cast<_TCHAR*>( _tcschr(strTempName, 92)); 

This is because the variant of the function used has const _TCHAR * as input and returns the constant _TCHAR *.

Another option would be to have strTempName declared as _TCHAR * and not as const _TCHAR *. In this case, a variant function is used that has the _TCHAR * parameter and returns the _TCHAR * value.

+4
source

_tcschr returns a const pointer. Therefore, the return value must be const _TCHAR* strGroupName = NULL; If it is not possible to change strGroupName to a const pointer, then declare both pointers as non-constant pointers.

+2
source

strGroupName must also be a pointer to const .

 const _TCHAR* strGroupName = _tcschr(strTempName, 92); 

You do not need to declare it before the call to initialize it.

+1
source

Source: https://habr.com/ru/post/1286267/


All Articles