c - 使用 char ** 指针的字符串操作代码给出了意外的结果

标签 c string pointers char-pointer

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

头文件是:test.h

#ifndef header_file
#define header_file

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

int increment(char **);

#endif  

源文件:main.c test.c

测试.c:

  1. 案例1:测试.c

    #include“test.h”

    整数增量(字符**字符串){

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

  2. 案例2:测试.c

    #include"test.h"

    整数增量(字符**字符串){

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

主.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;
}

生成文件是:

#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

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

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

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

最佳答案

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.

你做错了几件事。

  1. 我在任何地方都看不到用空格分隔单词,这似乎是您的主要要求。
  2. 但是,我看到一些对指针的疯狂操作。坦率地说,我无法理解它们背后的逻辑。

所以让我给你举个例子,告诉你如何做到这一点——完全不修改任何指针:

#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;
}

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

#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;
}

关于c - 使用 char ** 指针的字符串操作代码给出了意外的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30816162/

相关文章:

c - 在不创建新文件的情况下查找并替换文本文件中的特定字符串

c# - 带有自定义类键的慢字典

java - 删除第一个空格后的每个字符串的内容

c++ - 如何在 OpenCV 中设置 ROI?

c - 求 k 素数的个数;

c - 如何通过指针将结构元素结构传递给函数

c++ - 使用临时地址安全吗?

c++ - 将 "pointer to pointer"传递给模板函数

c - 初学者Cuda程序

c - 存储类声明