使用char **指针的字符串操作代码会产生意外结果

时间:2020-12-21 18:52:30

I was trying to implement a string parsing code for that I need substring of the given string so I deed the following:

我试图实现一个字符串解析代码,因为我需要给定字符串的子字符串,所以我需要执行以下操作:

The header file is : test.h

头文件是:test.h

#ifndef header_file
#define header_file

#include<stdio.h>
#include<string.h> 

int increment(char **);

#endif  

The source files : main.c test.c

源文件:main.c test.c

test.c :

test.c:

  1. Case1 :test.c

    案例1:test.c

    #include"test.h"

    #包括“test.h”

    int increment(char **string){

    int increment(char ** string){

        char *temp = *(string);
        int value;
    
    
        if(temp != NULL){
    
                *(string) = ++temp;
                value = 1;
        }
    
        else{
                value = 0;
        }
    
        return value;
    

    }

    }

  2. Case2 :test.c

    案例2:test.c

    #include"test.h"

    #包括“test.h”

    int increment(char **string){

    int increment(char ** string){

        char *temp = *(string);
        int value;
    
    
        if(*temp != '\0'){
    
                *(string) = ++temp;
                value = 1;
        }
    
        else{
                value = 0;
        }
    
        return value;
    

    }

    }

main.c:

main.c中:

#include"test.h"

int main()
{
        char str[30] = "I have done form here comes.";
        char strs[50];

        char *p = str;

        memset(strs, 0, 50);

        while(increment(&p))
        {
                strcpy(strs,p);
                printf("Originally the string is : %s\n", str);
                printf("The modified string is   : %s\n", strs);
                memset(strs, 0, 50);
        }

        return 0;
}

The makefile is :

makefile是:

#This is the makefile.

all : run main.o test.o

run : main.o test.o
        $(CC) -g $^ -o $@

%.o : %.c
        $(CC) -g -c $^ -o $@

.PHONY : clean

clean : 
        -rm -f *.o run

But in the first case in test.c where I tried to traversed the sub-string but It is giving some garbage result. And second case works fine.

但在test.c中的第一种情况下,我试图遍历子字符串,但它给出了一些垃圾结果。第二种情况很好。

What is going wrong in test.c case 1.

test.c案例1出了什么问题。

thanks!!!!!!!!!!!!

谢谢!!!!!!!!!!!!

1 个解决方案

#1


2  

The purpose was to add prefix and suffix to every word in the string so I need the string before the word , the word , and after the word. ex: prefix_I_suffix have done form here comes. I prefix_have_suffix have done form here comes.

目的是为字符串中的每个单词添加前缀和后缀,因此我需要在单词,单词和单词之后的字符串。 ex:prefix_I_suffix已经完成了表格。我在这里做了前缀_have_suffix表格。

You're doing a few things wrong.

你做错了几件事。

  1. I don't see splitting words by spaces anywhere at all, and that seems to be your main requirement.
  2. 我看不到任何地方的空格分裂,这似乎是你的主要要求。
  3. I see, however, some insane operations on pointers. Frankly, I can't understand the logic behind them.
  4. 但是,我看到一些针对指针的疯狂操作。坦率地说,我无法理解它们背后的逻辑。

So let me give you examples how it can be done - without modifying any pointers at all:

那么,让我举例说明如何完成它 - 不需要修改任何指针:

#include <stdio.h>
#include <string.h>

int main ()
{
    char str[] = "I have done answer here comes.";

    const char *delimiters = " ";
    char *pch = strtok(str, delimiters);
    while (pch != NULL)
    {
        printf("prefix_%s_suffix\n", pch);
        pch = strtok(NULL, delimiters);
    }
    return 0;
}

With copying the input to intermediate array that you can modify however you want:

将输入复制到中间数组,您可以根据需要进行修改:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
    char str[] = "I have done answer here comes.";

    char **array = NULL;
    size_t array_size = 0;

    const char *delimiters = " ";
    char *pch = strtok(str, delimiters);
    while (pch != NULL)
    {
        size_t str_size = strlen(pch) + 1; //to accommodate for `\0` byte
        char *str_copy = malloc(str_size);
        if (!str_copy)
        {
            printf("No memory!");
            return 1;
        }
        memcpy(str_copy, pch, str_size);

        ++ array_size;
        array = realloc(array, sizeof(char*) * array_size);
        if (!array)
        {
            printf("No memory!");
            return 1;
        }
        array[array_size - 1] = str_copy;

        pch = strtok(NULL, delimiters);
    }

    for (size_t i = 0; i < array_size; i ++)
    {
        printf("prefix_%s_suffix\n", array[i]);
        free(array[i]);
    }
    free(array);

    return 0;
}

#1


2  

The purpose was to add prefix and suffix to every word in the string so I need the string before the word , the word , and after the word. ex: prefix_I_suffix have done form here comes. I prefix_have_suffix have done form here comes.

目的是为字符串中的每个单词添加前缀和后缀,因此我需要在单词,单词和单词之后的字符串。 ex:prefix_I_suffix已经完成了表格。我在这里做了前缀_have_suffix表格。

You're doing a few things wrong.

你做错了几件事。

  1. I don't see splitting words by spaces anywhere at all, and that seems to be your main requirement.
  2. 我看不到任何地方的空格分裂,这似乎是你的主要要求。
  3. I see, however, some insane operations on pointers. Frankly, I can't understand the logic behind them.
  4. 但是,我看到一些针对指针的疯狂操作。坦率地说,我无法理解它们背后的逻辑。

So let me give you examples how it can be done - without modifying any pointers at all:

那么,让我举例说明如何完成它 - 不需要修改任何指针:

#include <stdio.h>
#include <string.h>

int main ()
{
    char str[] = "I have done answer here comes.";

    const char *delimiters = " ";
    char *pch = strtok(str, delimiters);
    while (pch != NULL)
    {
        printf("prefix_%s_suffix\n", pch);
        pch = strtok(NULL, delimiters);
    }
    return 0;
}

With copying the input to intermediate array that you can modify however you want:

将输入复制到中间数组,您可以根据需要进行修改:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
    char str[] = "I have done answer here comes.";

    char **array = NULL;
    size_t array_size = 0;

    const char *delimiters = " ";
    char *pch = strtok(str, delimiters);
    while (pch != NULL)
    {
        size_t str_size = strlen(pch) + 1; //to accommodate for `\0` byte
        char *str_copy = malloc(str_size);
        if (!str_copy)
        {
            printf("No memory!");
            return 1;
        }
        memcpy(str_copy, pch, str_size);

        ++ array_size;
        array = realloc(array, sizeof(char*) * array_size);
        if (!array)
        {
            printf("No memory!");
            return 1;
        }
        array[array_size - 1] = str_copy;

        pch = strtok(NULL, delimiters);
    }

    for (size_t i = 0; i < array_size; i ++)
    {
        printf("prefix_%s_suffix\n", array[i]);
        free(array[i]);
    }
    free(array);

    return 0;
}