Confusion about pointers in this lesson

At 14:26 when Michael was removing the magic numbers using %i, a pointer (*) wasn’t needed for HiddenWord.Len(). Can someone please explain to me why we need a pointer for string (%s with *HiddenWord) but not for integers (%i with HiddenWord.Len())? Thanks.

Another error I have encountered was when I tried to tackle the challenge. I got stuck with the problem I mentioned and tried the following code, which returned a ridiculously large negative number on the word length. Would be great to find out why this happens.

PrintLine(FString::Printf(TEXT(“Try to guess the %i letter word”), *HiddenWord, HiddenWord.Len()));

p.s. I have been able to follow the lessons so far but this lesson is by far the most confusing one. I simply don’t feel like I have grasp the concept where I can manipulate this code in my own ways. Right now, I am just copying whatever Michael is doing and that’s about it.

To avoid possible confusion *HiddenWord this does not make HiddenWord a pointer or get it’s address. FString has overloaded the dereference operator to do something different here.

FString is a class created by Epic and stores a TArray of TCHAR (correct char type for Unicode depending on platform). These chars are stored next to each other in memory and null terminated.

FString::Printf uses one of the printf family of functions from C. C deals with strings by a pointer to the first element of the array and the null \0 character is used to denote the end of it.
So printf will read characters until it reaches that and stop

(Sometimes C API’s take a pointer and length so you can pass a subrange e.g.
foo(string, 10) //only read first 10 characters)

FString::operator* is a way to get the char array it’s holding. With std::string this exists as a member function data().

https://godbolt.org/z/x56Gqn
This is essentially what is happening with * for FString

You supplied two arguments but you only have one format specifier

PrintLine(FString::Printf(TEXT(“Try to guess the %i letter word”), HiddenWord.Len()));
// or
PrintLine(FString::Printf(TEXT(“Try to guess the %i letter word (it's %s)”), HiddenWord.Len(), *HiddenWord));
2 Likes

Thanks for clarifying things up!

1 Like

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

Privacy & Terms