In this case Arr and ByCopy are temporary variables so you don’t want to return the address of invalid memory, you should pass the array you’re working with or access it if it’s available.
You can have it defined in your header or available before calling the function, for this I would recommend returning a pointer so you can check for valid index
int32* ACodeFiveCharacter::GetArrByRef(TArray<int32>& ArrRef, int32 index)
{
if(ArrRef.IsValidIndex(index))
{
return &ArrRef[index];
}
return nullptr;
}
TArray<int32> Arr = { 0, 1, 2 };
int32* ArrElem = GetArrByRef(Arr, 0); //Returns a pointer, can be null if index is not valid
int32& ArrElemRef = *ArrElem; //If you want to work with ref variable but be sure ArrElem is not null.