【问题标题】:c passing pointers to functions to handle char arrays and datac 将指针传递给函数以处理 char 数组和数据
【发布时间】:2016-01-02 21:25:01
【问题描述】:

我正在编写一些处理字符串的函数,并发现我不能只将指针传递给函数并在其中进行 malloc,因为它不起作用。

例如:

char* string;
void create_string(char* output, char* text) {
   len = strlen(text);
   output = (char*)calloc(1, len);
   strncpy(output, "test", len);
}

这有点令人费解,但无论如何它都行不通。我需要传递一个指向这样的指针的指针:

char* string;
void create_string(char** output, char* text) {
   len = strlen(text);
   *output = (char*)calloc(1, len);
   strncpy(*output, "test", len);
}

并使用指针取消引用地址。好的,没关系。

接下来我想做一些类似于读取文件的函数。这个功能很好用。

char* ru_read_file(char* data, const char* file_path) {
    FILE* fp;
    size_t buffer = 4096;
    size_t index = 0;
    int ch;

    fp = fopen(file_path, "r");
    if (fp == NULL) {
        printf("failed to open file: %s\n", file_path);
        return "-1\0";
    }
    printf("filepath: %s\n",file_path);

    data = (char*)malloc(sizeof(char) * buffer);
    while (EOF != (ch = fgetc(fp))) {
        data[index] = (char)ch;
        ++index;
        if (index == buffer - 1) {
            buffer = buffer * 2;
            data = realloc(data, buffer);
            if (data != NULL) {
                printf(
                    "buffer not large enough, reallocating %zu bytes to "
                    "load %s\n",
                    buffer, file_path);
            } else {
                printf("failed to realloc %zu bytes to load %s\n", buffer,
                       file_path);
            }
        }
    }
    data = realloc(data, (sizeof(char) * (index + 1)));
    data[index] = '\0';

    fclose(fp);
    return data;
}

上面的函数可以正常工作并且符合我的期望。接下来尝试将指向指针的指针作为函数的第一个参数传递,但我无法绕开它使其工作。

我虽然通过一个简单的指针来更新变量以通过指针取消引用它是可行的,但我遇到了段错误。

这里是代码

char* ru_read_file(char** data, const char* file_path) {
    FILE* fp;
    size_t buffer = 4096;
    size_t index = 0;
    int ch;

    fp = fopen(file_path, "r");
    if (fp == NULL) {
        printf("failed to open file: %s\n", file_path);
        return "-1\0";
    }
    printf("filepath: %s\n",file_path);

    data = (char**)malloc(sizeof(char) * buffer);
    while (EOF != (ch = fgetc(fp))) {
        *data[index] = (char)ch;
        ++index;
        if (index == buffer - 1) {
            buffer = buffer * 2;
            *data = realloc(data, buffer);
            if (*data != NULL) {
                printf(
                    "buffer not large enough, reallocating %zu bytes to "
                    "load %s\n",
                    buffer, file_path);
            } else {
                printf("failed to realloc %zu bytes to load %s\n", buffer,
                       file_path);
            }
        }
    }
    *data = realloc(*data, (sizeof(char) * (index + 1)));
    *data[index] = '\0';

    fclose(fp);
    return *data;
}

如何将指针传递给指针并像工作示例一样使用它?我宁愿避免读取数据然后将其复制到另一个缓冲区。

【问题讨论】:

  • 不要将malloc & 朋友的结果投射到 C 中!在使用双指针之前,您应该使用其他机制。 函数有一个很好的特性:它们可以返回一个值。
  • 请不要发布大量有效的代码。专注于没有的代码。

标签: c arrays pointers memory-management


【解决方案1】:

您必须调用malloc,就像在您的第二个小示例中一样,并且不要转换malloc 和朋友的结果。并始终检查malloc 的结果。因此,换行:

data = (char**)malloc(sizeof(char) * buffer);

*data = malloc(sizeof(char) * buffer). 
if(*data == NULL) { // error handling
}

此外,您必须在索引它之前取消引用data,因为后者的运算符具有更高的优先级。也就是替换

*data[index] = (char)ch;

(*data)[index] = (char) ch;

同样适用于接近尾声的地方,它必须是:

(*data)[index] = '\0';

最后,您根本不需要这个双指针,因为您可以将分配的内存作为char * 返回。也就是说,回到您的工作示例,定义一个局部变量 char *data 而不是 一个参数和 return data; 最后。

【讨论】:

  • 我不得不承认我在指针方面非常糟糕,但感谢您的回复,我能够解决它。
  • 感谢您编辑后的答案,我想我会回到前面的示例,因为在尝试使用双指针后它似乎更清晰了。我非常感谢您提供的额外信息!
【解决方案2】:

换行

data = (char**)malloc(sizeof(char) * buffer);

*data = (char*)malloc(sizeof(char) * buffer);

并尝试获得更多关于指针如何工作的理论知识。

【讨论】:

  • 坦率地说,我不会使用 any 演员表。
猜你喜欢
  • 2021-12-02
  • 2016-03-27
  • 1970-01-01
  • 2012-07-28
  • 1970-01-01
  • 2011-05-22
  • 2021-09-07
  • 1970-01-01
相关资源
最近更新 更多