【问题标题】:Reading char to (char pointer)将 char 读入(char 指针)
【发布时间】:2014-07-23 00:33:12
【问题描述】:

如何将其转换为返回 char* 而不使用 std::string 只是想在没有 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;
}

【问题讨论】:

  • 通过查看std:string 的文档。提示:.c_str() - 你可能也想使用strdup
  • 为什么要这样做?如果您要返回char*,则必须分配并稍后释放内存。另外,您必须记住 C 字符串以 \0 空字符结尾。真的没有理由这样做。
  • ReadCharArrayChar[i](); 是怎么回事?
  • 我不想看到没有 std::string 的其他方式。我只是一起完成了这件事,就像我正在做的事情一样 ReadCharArrayChar[i]();只是一个例子

标签: c++ c


【解决方案1】:

其他方式都很难看,这也是std::string 存在的原因之一:)。但出于教育目的,您可以通过以下方式返回 char*(按要求):

// 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;
}

类似的选项仍然使用原始指针作为缓冲区,但让调用者将其传入(连同其大小):

// 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;
}

后一个选项将允许,例如:

char buffer[100];
getEntityName(getAddress(), buffer, 100);

【讨论】:

  • 不需要删除新的 char[size] 吗?
  • @Nathan 调用代码最终需要这样做。
  • 那是用 std::string 做的最好方法吗?
  • @Nathan 你的意思是out std::string? “最佳”总是取决于上下文,但如果我被禁止使用任何类型的字符串对象,我可能会这样做。另一种选择是让调用者传入缓冲区和最大大小。这将使您在存储字符的方式和位置方面更加灵活。
【解决方案2】:
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;
}

请注意,调用者在完成返回值后应该free。这假设size 包括终止零字节,它似乎给出了示例代码。

【讨论】:

  • 赞成,但如果我没记错的话,malloc 的结果需要显式转换为 char*
  • @dlf malloc 返回 void *,因此不需要强制转换。
  • @FiddlingBits 我的编译器(VC2012)不会隐式执行该转换。但是,我(在这里)了解到,我的编译器会或不会做的事情并不总是正确性的最佳晴雨表...... :)
  • @FiddlingBits 但也许this time it's right
  • @dlf 在 C99 发布时应该不再需要了……我想。 :-D
【解决方案3】:

如果我绝对不能使用std::string,我会为此使用std::vector&lt;char&gt;

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

std::unique_ptr&lt;char[]&gt;,如果我知道调用者永远不需要修改结果:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-17
    • 2012-10-04
    • 2018-04-05
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多