user-0 提问: 求子串操作 求高手

回复:
user: user-0
content: 回复上面答主问题，重新释放是因为防止存放新子串的空间不足，故重新分配

user: user-0
content: 附上修改好的代码，原因是我把malloc函数写在了while里面，这样的话每次str #include <stdio.h> #include <stdlib.h>  /* run this program using the console pauser or add your own getch, system("pause") or input loop */  int main(int argc, char *argv[]) { 	int* i;     i=(int*)malloc(sizeof(int)*10); 	printf("%x\n",i); 	i=(int*)malloc(sizeof(int)*10); 	printf("%x",i);  	return 0; } 可以看到结果是：4c2e98 4c0c50  所以对比原函数，每次while 都开辟了新的地址空间，str指向了新的地址，所以再去那个地址找ch[0],ch[1],ch[2]，就只有一个str.ch[2]可以被找到了。应该把malloc写在while外面

user: user-0
content: #include <stdio.h> #include <stdlib.h>  /* run this program using the console pauser or add your own getch, system("pause") or input loop */ typedef struct{ 	char* ch; 	int length; }Str; int substring(Str str,Str* substr1,Str* substr2,int pos,int end) { 	if(pos<0||end>str.length||pos>end) 	return 0; 	if(substr1->ch) 	{ 		free(substr1->ch); 		substr1->ch=NULL; 	} 	if(substr2->ch) 	{ 		free(substr2->ch); 		substr2->ch=NULL; 	} 	int i=0;int j=end+1; 	substr1->ch=(char*)malloc(sizeof(char)*pos); 	 	while(i<pos) 	{ 	 		substr1->ch[i]=str.ch[i]; 		i++; 	} 	int k=0; 	substr2->ch=(char*)malloc(sizeof(char)*(str.length-end)); 	while(j<=str.length) 	{ 		substr2->ch[k++]=str.ch[j++]; 	} 	substr1->ch[pos]='\0'; 	return 1;  } int main() { 	int a=0; 	Str str1; 	Str str; 	 str.length=8;     str.ch=(char*)malloc(sizeof(char)*(str1.length));     str.ch="ABACCCEFFDH";     str1.length=6;     str1.ch=(char*)malloc(sizeof(char)*(str1.length));     str1.ch="ABCD";     Str str2;     str2.length=2;     str2.ch=(char*)malloc(sizeof(char)*(str2.length));     str2.ch="CD";   	a=substring(str,&str1,&str2,3,5);    	printf("%d",a);    	printf("%s\n",str1.ch);    	printf("%s\n",str2.ch); 	printf("在第%d个位置匹配成功",a);  	return 0; }

user: user-1
content: #include <stdio.h> #include <stdlib.h> #include <string.h>  /* run this program using the console pauser or add your own getch, system("pause") or input loop */ typedef struct{     char* ch;     int length; }Str;  int substring(Str str, Str* substr1, Str* substr2, int pos, int end) {     if(pos<0||end>str.length||pos>end)         return 0;     if(substr1->ch)     {         printf("free substr1\n");         free(substr1->ch);         substr1->ch=NULL;     }     if(substr2->ch)     {         printf("free substr2\n");         free(substr2->ch);         substr2->ch=NULL;     }      int i=0;int j=end+1;     while(i<pos)     {         substr1->ch=(char*)malloc(sizeof(char)*pos);         substr1->ch[i]=str.ch[i];         printf("i=%d\n",i);         printf("%c\n",substr1->ch[i]);         i++;      }     int k=0;     while(j<=str.length)     {         substr2->ch=(char*)malloc(sizeof(char)*(str.length-end));         printf("k=%d\n",k);         printf("j=%d\n",j);          substr2->ch[k++]=str.ch[j++];         printf("substr2=%c\n",substr2->ch[k]);      }     //substr1->ch[pos]='\0';     int m;     for(m=0;m<3;m++)     {         printf("%c\n",substr1->ch[m]);         printf("m=%d\n",m);     }      //printf("%s\n",substr2->ch);     return 1;  } int main() {     int a=0;     Str str1;     Str str;     str.length=8;     str.ch=(char*)malloc(sizeof(char)*(str1.length));     strcpy(str.ch, "ABACCCEF");     //str.ch="ABACCCEF";     str1.length=6;     str1.ch=(char*)malloc(sizeof(char)*(str1.length));     strcpy(str1.ch, "ABCD");     //str1.ch="ABCD";     Str str2;     str2.length = 3;     str2.ch=(char*)malloc(sizeof(char)*(str2.length));     strcpy(str2.ch, "CD");     //str2.ch="CD";     //  a=concat(str,&str1,&str2);     a=substring(str,&str1,&str2,3,5);     printf("%d",a);     // printf("%s\n",str1.ch);     // printf("%s\n",str2.ch);     //   printf("在第%d个位置匹配成功",a);      return 0; } 看不懂你的定义为寻找字串的函数是在做什么。 字符串不能直接赋值，需要借用strcpy, strncpy. 自定义函数里把已经申请空间的变量为什么进入自定义函数里马上释放呢？逻辑比较混乱。建议，你用文字把你的需求描述清楚一些～

user: user-0