使用 char ** 指针的字符串操作代码给出了意外的结果
String manipulation code using char ** pointer gives unexpected result
我正在尝试实现一个字符串解析代码,因为我需要给定字符串的子字符串,所以我执行以下操作:
头文件是:test.h
#ifndef header_file
#define header_file
#include<stdio.h>
#include<string.h>
int increment(char **);
#endif
源文件:main.ctest.c
test.c :
案例 1 :test.c
#include"test.h"
整数增量(字符**字符串){
char *temp = *(string);
int value;
if(temp != NULL){
*(string) = ++temp;
value = 1;
}
else{
value = 0;
}
return value;
}
案例2:test.c
#include"test.h"
整数增量(字符**字符串){
char *temp = *(string);
int value;
if(*temp != '[=12=]'){
*(string) = ++temp;
value = 1;
}
else{
value = 0;
}
return value;
}
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;
}
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
但在 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.
你做错了几件事。
- 我在任何地方都看不到用空格分隔单词,这似乎是您的主要要求。
- 但是,我看到一些对指针的疯狂操作。说实话,我看不懂他们背后的逻辑。
所以让我给你举个例子如何做到这一点 - 根本不修改任何指针:
#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 `[=11=]` 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;
}
我正在尝试实现一个字符串解析代码,因为我需要给定字符串的子字符串,所以我执行以下操作:
头文件是:test.h
#ifndef header_file
#define header_file
#include<stdio.h>
#include<string.h>
int increment(char **);
#endif
源文件:main.ctest.c
test.c :
案例 1 :test.c
#include"test.h"
整数增量(字符**字符串){
char *temp = *(string); int value; if(temp != NULL){ *(string) = ++temp; value = 1; } else{ value = 0; } return value;
}
案例2:test.c
#include"test.h"
整数增量(字符**字符串){
char *temp = *(string); int value; if(*temp != '[=12=]'){ *(string) = ++temp; value = 1; } else{ value = 0; } return value;
}
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;
}
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
但在 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.
你做错了几件事。
- 我在任何地方都看不到用空格分隔单词,这似乎是您的主要要求。
- 但是,我看到一些对指针的疯狂操作。说实话,我看不懂他们背后的逻辑。
所以让我给你举个例子如何做到这一点 - 根本不修改任何指针:
#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 `[=11=]` 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;
}