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

C尝试将指针复制到新指针,c,pointers,C,Pointers,我需要一些帮助: 我试图从指针中删除一个单词,并将其放入具有新长度的新指针中,但无法将其复制到新指针 我不确定什么时候应该使用free()函数。 当我在delete函数中使用free(str)时,它崩溃了 在我将“str”复制到“newStr”之后,用新的长度将“newStr”复制回“str”的最佳方法是什么 请帮助我理解它,我是新来的,我在谷歌上搜索过它,我试着在这里寻找,但没有找到能帮助我的东西 void delete(char *str) { int i, indexStart =

我需要一些帮助:

  • 我试图从指针中删除一个单词,并将其放入具有新长度的新指针中,但无法将其复制到新指针

  • 我不确定什么时候应该使用
    free()
    函数。 当我在delete函数中使用free(str)时,它崩溃了

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

  • 请帮助我理解它,我是新来的,我在谷歌上搜索过它,我试着在这里寻找,但没有找到能帮助我的东西

    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");
    }
    
    void删除(char*str)
    {
    int i,indexStart=0,indexEnd=0,wordlen=0,newLen=0,len=0;
    printf(“输入要删除的单词的索引:”);
    scanf(“%d”、&i);
    索引(i、str和indexStart、indexEnd和wordlen);
    len=strlen(str);
    newLen=len-wordlen-1;
    char*newStr=(char*)malloc(newLen*sizeof(char));
    if(newStr==NULL)
    {
    printf(“错误!未分配内存”);
    出口(0);
    }
    对于(int j=0;j索引结束)
    {
    *newStr=*str;
    newStr++;
    }
    str++;
    }
    自由基(str);
    //免费(newStr);
    printf(“新字符串:%s\n”,newStr);
    }
    void main()
    {
    char*str=(char*)malloc(1*sizeof(char));
    如果(str==NULL)
    {
    printf(“错误!未分配内存”);
    出口(0);
    }
    文本(str);
    如果(str!=NULL)
    {
    删除(str);
    }
    自由基(str);
    系统(“暂停”);
    }
    
    根据您的功能,您应该解决小的独立任务。但是您的函数
    delete()
    打印到控制台,扫描输入,分配新字符串并填充新字符串。但更糟糕的是调用此函数中的
    exit()
    。如果出现错误,函数必须返回错误,但不能停止程序。此外,函数的名称也应该反映它们的功能

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

    这是一个工作代码:

    #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;
    }
    
    #包括
    #包括
    #包括
    #包括
    常量字符*findWord(常量字符*str,常量字符*分隔符,无符号索引){
    //验证输入参数
    如果(!str | |!分隔符){
    返回NULL;
    }
    //数词
    无符号计数=0;
    while(*str&&count<索引){
    //跳过定界符
    while(*str&&!strhr(分隔符,*str)){
    str++;
    }
    如果(*str){
    计数++;
    }
    //跳过单词
    while(*str&&strhr(分隔符,*str)){
    str++;
    }
    }
    //如果索引太大,则返回NULL
    返回*str?str:NULL;
    }
    无符号countLengthOfWord(常量字符*str,常量字符*分隔符){
    //验证输入参数
    如果(!str | |!分隔符){
    返回0;
    }
    //计数长度
    无符号长度=0;
    while(*str&&!strhr(分隔符,*str++){
    长度++;
    }
    返回长度;
    }
    char*cutWord(char*str、const char*分隔符、无符号索引){
    //验证输入参数
    如果(!str){
    返回NULL;
    }
    str=(char*)findWord(str,分隔符,索引);
    //如果索引太大,则返回NULL
    如果(!str){
    返回NULL;
    }
    //为word分配新字符串
    无符号长度=countLengthOfWord(str,分隔符);
    char*word=malloc(长度+1);
    //检查分配是否成功
    如果(!word){
    返回NULL;
    }
    //抄袭词
    strncpy(单词、str、长度);
    字[长度]='\0';
    //从字符串中删除单词
    const char*ptr=str+length;
    while(*ptr){
    *str++=*ptr++;
    }
    *str='\0';
    返回词;
    }
    int main(){
    char str1[]=“你好,我的世界!”;
    char str2[]=“你好,我的世界!”;
    char str3[]=“你好,我的世界!”;
    char*word1=cutWord(str1,,!,0);
    char*word2=cutWord(str2,“,!”,1);
    char*word3=cutWord(str3,“,!”,2);
    如果(字1){
    printf(“单词:%s\n字符串:%s\n\n”,单词1,str1);
    }
    如果(字2){
    printf(“单词:%s\n字符串:%s\n\n”,单词2,str2);
    }
    如果(字3){
    printf(“单词:%s\n字符串:%s\n\n”,单词3,str3);
    }
    //释放分配的内存
    免费(word1);
    免费(word2);
    免费(word3);
    getchar();
    返回0;
    }
    
    您似乎忘记了在C中字符串实际上被称为以null结尾的字符串。您似乎还忘记了您应该只
    释放
    分配的内存一次。而函数的参数是按值传递的(即复制的)。要释放以前动态分配的内存,程序需要将它从分配函数(通常是
    malloc()
    calloc()
    realloc()
    )收到的(指针-)值准确地传递给
    free()
    )。
    free(newStr)的情况并非如此free(str),code>nor