The other ways are ugly, which is one of the reasons std::string
exists :). But for educational purposes, here is how you could return char*
(as asked):
// caller is responsible for deleting the return value
char* getEntityName(DWORD Address)
{
DWORD BaseDword = ReadBaseDword(Address); // (not sure what this achieves)
int size = ReadCharSize();
char* name = new char[size];
name[size - 1] = '\0';
for (int i = 0; i < size - 1; i++)
{
char c = ReadCharArrayChar[i](); // odd looking, but I'll assume this works
name[i] = c;
}
return name;
}
A similar option still uses a raw pointer for the buffer, but has the caller pass it in (along with its size):
// returns: true iff the buffer was successfully populated with the name
// exceptions might be a better choice, but let's keep things simple here
bool getEntityName(DWORD Address, char* buffer, int maxSize)
{
DWORD BaseDword = ReadBaseDword(Address); // (not sure what this achieves?)
int size = ReadCharSize();
if(size > maxSize)
return false;
buffer[size - 1] = '\0';
for (int i = 0; i < size - 1; i++)
{
char c = ReadCharArrayChar[i](); // odd looking, but I'll assume this works
buffer[i] = c;
}
return true;
}
The latter option would allow, for example:
char buffer[100];
getEntityName(getAddress(), buffer, 100);