#include "stdafx.h"
#define NULL 0
#define NUL '\0'
#define TRUE 1
#define FALSE 0 char *match(char *str, char *want);
int del_substr(char *str,char *sbu);
int _tmain(int argc, _TCHAR* argv[])
{
char *str1 = "ABCDEFGHIJK";
char *str2 = "CD";
printf("%s\n",str1); del_substr(str1,str2); printf("%s\n",str1);
return 0;}
char *match(char *str, char *want)
{
while(*want != NUL)
{ if(*str++ != *want++)
return NULL;
}
return str;
}int del_substr(char *str,char *sbu)
{
char *next = "0";
while(*str != NUL)
{
next = match(str,sbu);
if(next != NULL)
break;
str++;
} if(*str == NUL)
return FALSE; while(*next != NUL)
{
*str++ = *next++;
} return TRUE;
}上述代码在运行到 int del_substr(char *str,char *sbu)
中的 *str++ = *next++;这行发生错误 0x00411565 处未处理的异常: 0xC0000005: 写入位置 0x00415932 时发生访问冲突查了半天给出了结论都是指针没有初始化,但*next已经初始化了,不解???求助
#define NULL 0
#define NUL '\0'
#define TRUE 1
#define FALSE 0 char *match(char *str, char *want);
int del_substr(char *str,char *sbu);
int _tmain(int argc, _TCHAR* argv[])
{
char *str1 = "ABCDEFGHIJK";
char *str2 = "CD";
printf("%s\n",str1); del_substr(str1,str2); printf("%s\n",str1);
return 0;}
char *match(char *str, char *want)
{
while(*want != NUL)
{ if(*str++ != *want++)
return NULL;
}
return str;
}int del_substr(char *str,char *sbu)
{
char *next = "0";
while(*str != NUL)
{
next = match(str,sbu);
if(next != NULL)
break;
str++;
} if(*str == NUL)
return FALSE; while(*next != NUL)
{
*str++ = *next++;
} return TRUE;
}上述代码在运行到 int del_substr(char *str,char *sbu)
中的 *str++ = *next++;这行发生错误 0x00411565 处未处理的异常: 0xC0000005: 写入位置 0x00415932 时发生访问冲突查了半天给出了结论都是指针没有初始化,但*next已经初始化了,不解???求助
next是const的,值为字符串0
判断字符串的尾用'\0'
char *next = NULL;
str和next是同一个字符串中的不同位置,这样操作很容易出问题
{
char *next = NULL;
while(*str != NUL)
{
next = match(str,sbu);
if(next != NULL)
break;
str++;
} if(*str == NUL)
return FALSE; while(*next != NUL)
{
*str++ = *next++;
} return TRUE;
}