Asterisk '*' symbol to de-reference a TArray?

In the lecture BullCowGame lecture “TArray Functions .Num()” I tried this statement:

    for (int32 Index = 0; Index < 5; Index++)
        PrintLine(TEXT("%s"), Words[Index]);

but later found that I had to “de-reference” the var Words which is a TArray like this:

    for (int32 Index = 0; Index < 5; Index++)
        PrintLine(TEXT("%s"), *Words[Index]);

I found the compiler error with the first block of code to be extremely confusing and I would never have known to “de-reference” unless the instructor explicitly said to. Can someone explain what the term “de-reference” is and what’s going on with this example?

I pasted the confusing compile error log below for the first method for reference (error goes away when I add the * in the second example):

Running Mono...

Running bundled mono, version: Mono JIT compiler version (2018-06/bb3ae37d71a Fri Nov 16 17:12:11 EST 2018)
/Users/Shared/Epic Games/UE_4.25/Engine /Users/Shared/Epic Games/UE_4.25/Engine/Binaries/Mac
Building BullCowGameEditor...
[Upgrade] Using backward-compatible build settings. The latest version of UE4 sets the following values by default, which may require code changes:
[Upgrade]     bLegacyPublicIncludePaths = false                 => Omits subfolders from public include paths to reduce compiler command line length. (Previously: true).
[Upgrade]     ShadowVariableWarningLevel = WarningLevel.Error   => Treats shadowed variable warnings as errors. (Previously: WarningLevel.Warning).
[Upgrade]     PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs   => Set in build.cs files to enables IWYU-style PCH model. See (Previously: PCHUsageMode.UseSharedPCHs).
[Upgrade] Suppress this message by setting 'DefaultBuildSettings = BuildSettingsVersion.V2;' in BullCowGameEditor.Target.cs, and explicitly overriding settings that differ from the new defaults.
Performing 3 actions (5 in parallel)
[1/3] Compile BullCowCartridge.cpp
/Users/rbgross2014/Desktop/GameDev/Udemy_UE_course/03_BullCow_game/BullCowGame-starter-kit/Source/BullCowGame/BullCowCartridge.cpp:11:47: error: no viable conversion from 'const FString' to 'const TCHAR *' (aka 'const char16_t *')
    FFileHelper::LoadFileToStringArray(Words, WordListPath);
/Users/Shared/Epic Games/UE_4.25/Engine/Source/Runtime/Core/Public/Containers/UnrealString.h:197:2: note: candidate function
        operator FStringView() const;
/Users/Shared/Epic Games/UE_4.25/Engine/Source/Runtime/Core/Public/Misc/FileHelper.h:103:75: note: passing argument to parameter 'Filename' here
        static bool LoadFileToStringArray( TArray<FString>& Result, const TCHAR* Filename, EHashOptions VerifyFlags = EHashOptions::None );
1 error generated.

In your PrintLine, you aren’t actually dereferencing the array but in fact dereferencing the FString which enables access to the TCHAR array needed for the formatter.

This is the same with WordListPath - if this is an FString it also needs to be dereferenced for use with the FFileHelper::LoadFileToStringArray call so you should actually have FFileHelper::LoadFileToStringArray(Words, *WordListPath); instead

I hope this helps

To add a tiny bit to this. The [] operator has a higher precedence than the * operator.

So *Words[Index] is the same as *(Words[Index]).
Or to separate that statement into multiple variables:

FString Word = Words[Index];
const TCHAR* WordCharacterArray = *Word;
PrintLine(TEXT("%s"), WordCharacterArray);

So that is what is happening all on that one line.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms