| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 504 人关注过本帖
标题:这个程序错在哪里呢?
只看楼主 加入收藏
独孤鹏
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-4-3
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:4 
这个程序错在哪里呢?
/*关于删除字符串中的特定字母*/
#include<stdio.h>
main()
{
    char string[]="you are my best friend";
    char c='e';
    void delete1(char string[],char c);
    delete1(string,c);
    printf("%s",string);
}
void delete1(char string[30],char c)
{
    char *ptr=string[30],*ptr1;
    while(*(ptr++)!='\0')
    {
        if(*ptr==c)
        {
            ptr1=ptr;
            while(*ptr1!='\0')
            {
                *ptr1=*(++ptr);
            }
            
            ptr--;
        }
    }
}
搜索更多相关主题的帖子: include friend 字符串 字母 
2010-04-29 21:47
waterstar
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:984
专家分:2810
注 册:2010-2-12
收藏
得分:3 
    char *ptr=string[30],*ptr1;
此行错误,应该是char *ptr=string,*ptr1;
因为对这行语句来说,string[30]是一个值,而且这个值还是没有定义过的
赋值给指针变量的时候应该是一个地址的值,例如你也可以这样定义
char *ptr=&string[0],*ptr1;

冰冻三尺,非一日之寒;士别三日,不足刮目相看!
2010-04-29 21:59
gyx8899
Rank: 2
等 级:论坛游民
帖 子:56
专家分:91
注 册:2010-2-2
收藏
得分:5 
/*关于删除字符串中的特定字母*/
#include<stdio.h>
void delete1(char *string,char c)
{
    char *ptr=string,*ptr1,*ptr0;//ptr0,ptr1为中间变量指针
    while(*(ptr++)!='\0')
    {
        if(*ptr==c)
        {
            ptr0=ptr1=ptr;//保存ptr地址。
            while(*ptr1!='\0')
            {
                *(ptr1++)=*(++ptr);//ptr1也需要递增。
            }
            ptr=++ptr0;//ptr指针返回保存地址,并递增一个单位。
        }
    }
}
void main()
{
    char string[]="you are my best friend";
    char c='e';
    delete1(&string,c);
    printf("%s\n",string);
}
仔细对比一下,好好揣摩,体会在心中。
2010-04-29 22:36
gyx8899
Rank: 2
等 级:论坛游民
帖 子:56
专家分:91
注 册:2010-2-2
收藏
得分:0 
/*关于删除字符串中的特定字母*/
#include<stdio.h>
void delete1(char *string,char c)
{
    char *ptr=string,*ptr1,*ptr0;//ptr0,ptr1为中间变量指针
    while(*(ptr++)!='\0')
    {
        if(*ptr==c)
        {
            ptr0=ptr1=ptr;//保存ptr地址。
            while(*ptr1!='\0')
            {
                *(ptr1++)=*(++ptr);//ptr1也需要递增。
            }
            ptr=++ptr0;//ptr指针返回保存地址,并递增一个单位。
        }
    }
}
void main()
{
    char string[]="you are my best friend";
    char c='e';
    delete1(&string,c);
    printf("%s\n",string);
}
仔细对比一下,好好揣摩,体会在心中。
2010-04-29 22:45
赤那
Rank: 3Rank: 3
来 自:广东
等 级:论坛游侠
威 望:1
帖 子:127
专家分:178
注 册:2010-3-1
收藏
得分:2 
#include<stdio.h>
void delete1(char *string,char c)
{
    char *ptr=string,*ptr1,*ptr0;//ptr0,ptr1为中间变量指针
    while(*(ptr++)!='\0')
    {
        if(*ptr==c)
        {
            ptr0=ptr1=ptr;//保存ptr地址。
            while(*ptr1!='\0')
            {
                *(ptr1++)=*(++ptr);//ptr1也需要递增。
            }
            ptr=++ptr0;//ptr指针返回保存地址,并递增一个单位。
        }
    }
}
void main()
{
    char string[]="you are my best friend";
    char c='e';
    delete1(string,c);
    printf("%s\n",string);
}
2010-04-29 23:28
快速回复:这个程序错在哪里呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015540 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved