c - 在 C 中重新实现 split 函数

标签 c arrays string pointers

我一直在尝试编写一个函数,该函数将字符串作为一行接收并返回指向单词数组的指针。下面写的函数做了类似的事情 我如何重写以下代码1,但它应该比代码2更好,因为能够更改分隔符。然而,code1 可以工作,但在内存分配期间,相同的内存会被复制到单词数组中。从而造成单词重复。

代码 1:

char *split(const char *string) {
    char *words[MAX_LENGTH / 2];
    char *word = (char *)calloc(MAX_WORD, sizeof(char));
    memset(word, ' ', sizeof(char));
    static int index = 0;
    int line_index = 0;
    int word_index = 0;

    while (string[line_index] != '\n') {
        const char c = string[line_index];
        if (c == ' ') {
            word[word_index+ 1] = '\0';
            memcpy(words + index, &word, sizeof(word));
            index += 1;
            if (word != NULL) {
                free(word);
                char *word = (char *)calloc(MAX_WORD, sizeof(char));
                memset(word, ' ', sizeof(char));
            }
            ++line_index;
            word_index = 0;
            continue;
        }
        if (c == '\t')
            continue;
        if (c == '.')
            continue;
        if (c == ',')
            continue;

        word[word_index] = c;
        ++word_index;
        ++line_index;
    }

    index = 0;
    if (word != NULL) {
        free(word);
    }
    return *words;
}

代码 2:

char **split(char *string) {
    static char *words[MAX_LENGTH / 2];
    static int index = 0;
    // resetting words 
    for (int i = 0; i < sizeof(words) / sizeof(words[0]); i++) {
         words[i] = NULL;
    }
    const char *delimiter = " ";
    char *ptr = strtok(string, delimiter);
    while (ptr != NULL) {
        words[index] = ptr;
        ptr = strtok(NULL, delimiter);
        ++index;
    }
    index = 0;
    return words;
}

但是我注意到word+index的内存被重新分配到同一位置,从而导致单词重复。

最佳答案

strtok() 总是返回一个不同的指针到初始字符串中。这不会产生重复项,除非您使用相同的输入字符串(可能使用新内容)调用它两次。

但是,您的函数返回一个指向静态数组的指针,该数组在每次调用split()时都会被覆盖,从而使所有先前调用的结果无效。为了防止这种情况发生,

  • 在每次调用中分配新内存(必须由调用者释放):

    char *words = calloc(MAX_LENGTH / 2, 1);
    
  • 或者返回一个struct(总是按值复制):

    struct wordlist { char *word[MAX_LENGTH / 2]; };
    
    wordlist split(char *string)
    {
        wordlist list = {};
        /* ... */
        list.word[index] = /* ... */;
        /* ... */
        return list;
    }
    

关于c - 在 C 中重新实现 split 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59031273/

相关文章:

c - 错误 : expected '=' , ','、 ';'、 'asm' 或 '__attribute__' 之前的 'int'

python - 二维 numpy 数组搜索(相当于 Matlab 的 intersect 'rows' 选项)

Java:从 block 中的数组列表创建数组的有效方法

java - 使用转义 java 创建字符串

Javascript - 替换字符串文字中的转义字符

c - bool 函数无法正确比较数组

c - 以毫秒精度测量时间

c - 为什么这段代码在 64 位架构上会出现段错误,但在 32 位架构上却可以正常工作?

ruby - 计算 Ruby 数组中相同的字符串元素

java - 替换字符串中的提及