How can I cast FString to Char *?

I’ve seen several casting answers but none of them are working. I thing I have something that compiles but I am unsure if it is the correct way:

int ARPG_Database::BP_GetColumnIndex(int resultSet, FString columnName)
{
	return GetColumnIndex(resultSet, (char *)&columnName.GetCharArray());
}

int ARPG_Database::GetColumnIndex(int iResult, const char* columnName)
{

You probably want this:

int ARPG_Database::BP_GetColumnIndex(int resultSet, FString columnName)
{
    return GetColumnIndex(resultSet, TCHAR_TO_ANSI(*columnName));
}
 
int ARPG_Database::GetColumnIndex(int iResult, const char* columnName)
{

FString uses TCHAR internally, so this would be how we’d typically a write function taking a string pointer in UE4:

int ARPG_Database::BP_GetColumnIndex(int resultSet, FString columnName)
{
    return GetColumnIndex(resultSet, *columnName);
}
 
int ARPG_Database::GetColumnIndex(int iResult, const TCHAR* columnName)
{

Thanks for the clarification sir.

I tried to top answer but I’m getting:

ISO C++11 does not allow conversion from string literal to ‘char *’

The function (not mine) parameter is * char (not const), but I am confident the contents are not changed in the function call.

Nevermind. It turned out to be another parameter in the function that was generating the error. Using the above solution fixed the original problem and exposed another.

I wrote a class to help with this, http://blogs.winterleafentertainment.com/post/2015/05/05/string-casting-fun-in-unreal-engine-4

Should cover your problem.

1 Like