C试图将指针复制到新指针

时间:2017-10-03 09:33:50

标签: c pointers

我需要一些帮助:

  1. 我正在尝试从指针中删除一个单词,并将其放入一个新长度的新指针中,但我无法将其复制到新指针

  2. 我不确定何时应该使用free()功能。 当我在删除功能中使用free(str)时,它会崩溃。

  3. 将“str”复制到“newStr”之后,用新长度将“newStr”复制回“str”的最佳方法是什么?

  4. 请帮助我理解,我是新来的,我用Google搜索,我试着看这里,但找不到能帮到我的东西。

    void delete(char *str)
    {
        int i, indexStart = 0, indexEnd = 0, wordlen = 0, newLen = 0, len = 0;
        printf("Enter the index of the word that you want to remove:  ");
        scanf("%d", &i);
        indexs(i, str,&indexStart,&indexEnd,&wordlen);
        len = strlen(str);
        newLen = len - wordlen - 1;
        char *newStr = (char*)malloc(newLen * sizeof(char));
        if (newStr == NULL)
        {
            printf("Error! memory not allocated.");
            exit(0);
        }
        for (int j = 0; j < len; j++)
        {
            if (j< (indexStart - 1) || j > indexEnd)
            {
                *newStr = *str;
                newStr++;
            }
            str++;
        }
    
        free(str);
        //free(newStr);
        printf("The new string:  %s\n", newStr);
    }
    void main()
    {
        char *str = (char*)malloc(1 * sizeof(char));
        if (str == NULL)
        {
            printf("Error! memory not allocated.");
            exit(0);
        }
        text(str);
    
        if (str != NULL)
        {
            delete(str);
        }
        free(str);
        system("pause");
    }
    

1 个答案:

答案 0 :(得分:1)

根据https://tburleson-layouts-demos.firebaseapp.com/#/docs你的功能应该解决小的单独任务。但是你的函数delete()打印到控制台,扫描输入,分配新字符串并填充这个新字符串。但更糟糕的是这个函数中的调用exit()。如果出现问题,函数必须返回错误,但不能停止程序。函数的名称也应该反映它们的作用。

free()分配的每个内存块使用malloc()

所以,这是一个有效的代码:

#include <stddef.h>

#include <stdio.h>

#include <string.h>
#include <malloc.h>

const char *findWord(const char *str, const char *delimiters, unsigned index) {
    // validate input parameters
    if (!str || !delimiters) {
        return NULL;
    }

    // count words
    unsigned count = 0;
    while (*str && count < index) {
        // skip delimiters
        while (*str && !strchr(delimiters, *str)) {
            str++;
        }

        if (*str) {
            count++;
        }

        // skip word
        while (*str && strchr(delimiters, *str)) {
            str++;
        }
    }

    // if index is too big returns NULL
    return *str ? str : NULL;
}

unsigned countLengthOfWord(const char *str, const char *delimiters) {
    // validate input parameters
    if (!str || !delimiters) {
        return 0;
    }

    // count length
    unsigned length = 0;
    while (*str && !strchr(delimiters, *str++)) {
        length++;
    }

    return length;
}

char *cutWord(char *str, const char *delimiters, unsigned index) {
    // validate input parameters
    if (!str) {
        return NULL;
    }

    str = (char *)findWord(str, delimiters, index);

    // if index is too big, return NULL
    if (!str) {
        return NULL;
    }

    // allocate new string for word
    unsigned length = countLengthOfWord(str, delimiters);
    char *word = malloc(length + 1);

    // check if allocation was successfull
    if (!word) {
        return NULL;
    }

    // copy word
    strncpy(word, str, length);
    word[length] = '\0';

    // cut word from string
    const char *ptr = str + length;
    while (*ptr) {
        *str++ = *ptr++;
    }
    *str = '\0';

    return word;
}

int main() {
    char str1[] = "Hello, my world!";
    char str2[] = "Hello, my world!";
    char str3[] = "Hello, my world!";

    char *word1 = cutWord(str1, " ,!", 0);
    char *word2 = cutWord(str2, " ,!", 1);
    char *word3 = cutWord(str3, " ,!", 2);

    if (word1) {
        printf("word: %s\nstring: %s\n\n", word1, str1);
    }

    if (word2) {
        printf("word: %s\nstring: %s\n\n", word2, str2);
    }

    if (word3) {
        printf("word: %s\nstring: %s\n\n", word3, str3);
    }

    // release allocated memory
    free(word1);
    free(word2);
    free(word3);

    getchar();

    return 0;
}