c指针问题求大神
程序目的是查找字符串1中是否有子字符串2,若有,则删除第一个子字符串2先上代码:#include<stdio.h>intdel_substr(char*str,charc...
程序目的是查找字符串1中是否有子字符串2,若有,则删除第一个子字符串2
先上代码:
#include <stdio.h>
int del_substr(char *str , char const *substr);
int main()
{
char *str1 = "asdfghjkl";
char *str2 = "dfg";
del_substr(str1 , str2);
printf("%s\n" , str1);
return 0;
}
int del_substr(char *str , char const *substr)
{
char *currChar = str;
char *currSubChar = (char *)substr;
int subLen = 0;
if (*substr == 0) //空串
return 1;
while(*currChar)
{
/*匹配目标字符串中是否有子字符串*/
if ( *currChar == *currSubChar)
for (subLen = 0 ; *(currSubChar +subLen ); subLen ++)
if ( *(currChar + subLen) != *(currSubChar + subLen ) )
break;
/*成功匹配到字符串,进行移动处理*/
if ( !*(currSubChar + subLen) )
{
for ( ; *currChar ; currChar ++)
*currChar = *(currChar + subLen );
* ++currChar = 0; //给字符串结尾
return 1;
}
currChar ++;
}
return 0;
}
错误提示:
错误代码:
*currChar = *(currChar + subLen );
反汇编代码:
*currChar = *(currChar + subLen );
00DB148E mov eax,dword ptr [currChar]
00DB1491 add eax,dword ptr [subLen]
00DB1494 mov ecx,dword ptr [currChar]
00DB1497 mov dl,byte ptr [eax]
00DB1499 mov byte ptr [ecx],dl
00DB149B jmp del_substr+0ABh (0DB147Bh)
求大神看看这是怎么回事? 展开
先上代码:
#include <stdio.h>
int del_substr(char *str , char const *substr);
int main()
{
char *str1 = "asdfghjkl";
char *str2 = "dfg";
del_substr(str1 , str2);
printf("%s\n" , str1);
return 0;
}
int del_substr(char *str , char const *substr)
{
char *currChar = str;
char *currSubChar = (char *)substr;
int subLen = 0;
if (*substr == 0) //空串
return 1;
while(*currChar)
{
/*匹配目标字符串中是否有子字符串*/
if ( *currChar == *currSubChar)
for (subLen = 0 ; *(currSubChar +subLen ); subLen ++)
if ( *(currChar + subLen) != *(currSubChar + subLen ) )
break;
/*成功匹配到字符串,进行移动处理*/
if ( !*(currSubChar + subLen) )
{
for ( ; *currChar ; currChar ++)
*currChar = *(currChar + subLen );
* ++currChar = 0; //给字符串结尾
return 1;
}
currChar ++;
}
return 0;
}
错误提示:
错误代码:
*currChar = *(currChar + subLen );
反汇编代码:
*currChar = *(currChar + subLen );
00DB148E mov eax,dword ptr [currChar]
00DB1491 add eax,dword ptr [subLen]
00DB1494 mov ecx,dword ptr [currChar]
00DB1497 mov dl,byte ptr [eax]
00DB1499 mov byte ptr [ecx],dl
00DB149B jmp del_substr+0ABh (0DB147Bh)
求大神看看这是怎么回事? 展开
1个回答
展开全部
#include <stdio.h>
int del_substr(char *str , char const *substr);
int main()
{
char str1[] = "asdfghjkl";//<----------------------------------
char *str2 = "dfg";
del_substr(str1 , str2);
printf("%s\n" , str1);
return 0;
}
int del_substr(char *str , char const *substr)
{
char *currChar = str;
char *currSubChar = (char *)substr;
int subLen = 0;
if (*substr == 0) //空串
return 1;
while(*currChar)
{
/*匹配目标字符串中是否有子字符串*/
if ( *currChar == *currSubChar)
for (subLen = 0 ; *(currSubChar +subLen ); subLen ++)
if ( *(currChar + subLen) != *(currSubChar + subLen ) )
break;
/*成功匹配到字符串,进行移动处理*/
if ( !*(currSubChar + subLen) )
{
for ( ; *currChar ; currChar ++)
*currChar = *(currChar + subLen );
* ++currChar = 0; //给字符串结尾
return 1;
}
currChar ++;
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询