Nathan
Nathan

Reputation: 209

Reading char to (char pointer)

how would i convert this to return char* an not use std::string just want to learn other ways to do this without std::string

string getName(DWORD Address)
{
    DWORD BaseDword = ReadBaseDword(Address);

    int size = ReadCharSize();

    string name = "";

    for (int i = 0; i < size - 1; i++)
    {
        char c = ReadCharArrayChar(i);
        name += c;
    }

    return name;
}

Upvotes: 0

Views: 313

Answers (3)

dlf
dlf

Reputation: 9383

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);

Upvotes: 4

Casey
Casey

Reputation: 42554

If I absolutely could not use std::string, I would either use std::vector<char> for this:

std::vector<char> getName(DWORD Address)
{
    DWORD BaseDword = ReadBaseDword(Address);

    const int size = ReadCharSize();

    std::vector<char> name(size);

    for (int i = 0; i < size - 1; i++)
    {
        name[i] = ReadCharArrayChar(i);
    }

    name[size - 1] = '\0';

    return name;
}

or std::unique_ptr<char[]> if I knew the caller would never need to modify the result:

std::unique_ptr<char[]> getName(DWORD Address)
{
    DWORD BaseDword = ReadBaseDword(Address);

    const int size = ReadCharSize();

    std::unique_ptr<char[]> name(new char[size]);

    for (int i = 0; i < size - 1; i++)
    {
        name[i] = ReadCharArrayChar(i);
    }

    name[size - 1] = '\0';

    return name;
}

Upvotes: 1

David Schwartz
David Schwartz

Reputation: 182769

char * getEntityName(DWORD Address)
{
    DWORD BaseDword = ReadBaseDword(Address);

    int size = ReadCharSize();

    char* name = malloc (size);

    for (int i = 0; i < size - 1; i++)
    {
        name[i] = ReadCharArrayChar[i]();
    }

    name[size - 1] = 0;

    return name;
}

Note that the caller should free the returned value when they're done with it. This assumes size includes the terminating zero byte, which it seems to given the example code.

Upvotes: 2

Related Questions