野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8143|回复: 4

求教《C和指针》第六章练习题2

[复制链接]
发表于 2016-4-21 16:17:38 | 显示全部楼层 |阅读模式
请各位指教,《C和指针》第六章练习题2,找出字符可以运行,但要删除时总是报不可写的错误,printf *pe的内容却是想要的字符,请教各位问题在哪?

int del_substr(char *str,char const *substr)
{
char subchar=*substr;
char ch=*str;
char *ps;
char *pe;
ps=str;
pe=str;
    if(ch=='\0')
  return 0;
/**
*找出是否有substr
*/
while (subchar!='\0')
{
  if(ch==subchar)
  {
   ps=str;
   while (ch=*++str, (subchar =*++substr)!='\0')
   {
    if(ch !=subchar)
    return 0;
   }
  }
  else
   ch=*++str;
}
/**
*如果找到了,就删除在str中的substr
*/
pe=str;
while ( *pe!='\0')
{
  *ps=*pe; /*运行到此就会出地址不可以写  Unhandled exception at 0x011444AC in stm.exe: 0xC0000005: Access violation writing location 0x01146888.*/
  ps++;
  pe++;
}
*ps='\0';

return 1;
}
回复

使用道具 举报

发表于 2016-4-21 19:44:27 | 显示全部楼层
感觉语法没问题
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-21 21:07:47 | 显示全部楼层
flyleaf 发表于 2016-4-21 19:44
感觉语法没问题

我是在win7系统下,用vs2012编译运行,会不会与编译器有关系
回复 支持 反对

使用道具 举报

发表于 2016-4-22 09:45:07 | 显示全部楼层
hudaliao 发表于 2016-4-21 21:07
我是在win7系统下,用vs2012编译运行,会不会与编译器有关系

不要怀疑编译器。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-22 10:06:29 | 显示全部楼层
flyleaf 发表于 2016-4-22 09:45
不要怀疑编译器。。

实在找不出原因,我也试过用中间变量,都会报这个错
*pe, *ps的值也都正确。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-3 15:14 , Processed in 0.032271 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表