| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 783 人关注过本帖, 1 人收藏
标题:用指针的方式对一个结构数组里的元素进行删除,再显示出来,不会了,谢谢大 ...
只看楼主 加入收藏
a18838132258
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-2-22
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:1 
用指针的方式对一个结构数组里的元素进行删除,再显示出来,不会了,谢谢大佬
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Tongxunlu{
        char *name;
        char *sex;
        char *number;
    };

void xianshi(struct Tongxunlu *p,int n);
void caxun1(struct Tongxunlu *p,int n);
void caxun2(struct Tongxunlu *p,int n);
void tianjia(struct Tongxunlu *p,int *n);
void shanchu(struct Tongxunlu *p,int *n);

int main(){
    struct Tongxunlu student[]={
        {"王明","男","13598832373"},
        {"李芳","女","13243552545"},
        {"李华","男","15765463662"},
        {"小明","男","18842352552"},
        {"小兰","女","16765645646"},
        {"小王","男","17343244324"},
        {"王凯","男","17743244324"}
        };
    int n = sizeof(student)/sizeof(student[0]);
    printf("%d",n);
    shanchu(student,&n);
    xianshi(student,n);
    return 0;
}

void xianshi(struct Tongxunlu *p,int n){
    printf("%d",n);
    p = (Tongxunlu*)malloc(sizeof(Tongxunlu)*n);
    printf("\n\t*********班级通讯录*********\n\n");
    printf("\t姓名\t性别\t  电话号码\n");
    while(n--){
            printf("\t%s\t %s\t%s\n",p->name,p->sex,p->number);
            p++;
    }
}

void shanchu(struct Tongxunlu *p,int *n){
    char name[10];
    int m=*n;
    while(m--){
            printf("\t%s\t %s\t%s\n",p->name,p->sex,p->number);
            p++;
    }
    printf("请输入需要删除的姓名:");
    scanf("%s",name);
    while(m--){
        if(strcmp(name,p->name)!=0){
            *p=*p;
            p++;
            }
    }
    *n=*n-1;
    m=m-1;
    while(m--){
            printf("\t%s\t %s\t%s\n",p->name,p->sex,p->number);
            p++;
    }
}
搜索更多相关主题的帖子: printf name void int struct 
2020-02-22 11:58
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10609
专家分:43210
注 册:2014-5-20
收藏
得分:20 
图片附件: 游客没有浏览图片的权限,请 登录注册

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Tongxunlu
{
    char *name;
    char *sex;
    char *number;
};

void xianshi(struct Tongxunlu *p,int n);
void caxun1(struct Tongxunlu *p,int n);
void caxun2(struct Tongxunlu *p,int n);
void tianjia(struct Tongxunlu *p,int *n);
void shanchu(struct Tongxunlu *p,int *n);

int main()
{
    struct Tongxunlu student[]=
    {
        {"王明","男","13598832373"},
        {"李芳","女","13243552545"},
        {"李华","男","15765463662"},
        {"小明","男","18842352552"},
        {"小兰","女","16765645646"},
        {"小王","男","17343244324"},
        {"王凯","男","17743244324"}
    };
    int n = sizeof(student)/sizeof(student[0]);
    shanchu(student,&n);
    xianshi(student,n);
    return 0;
}

void xianshi(struct Tongxunlu *p,int n)
{
    printf("%d",n);
    printf("\n\t*********班级通讯录*********\n\n");
    printf("\t姓名\t性别\t  电话号码\n");
    while(n--)
    {
        printf("\t%s\t %s\t%s\n",p->name,p->sex,p->number);
        p++;
    }
}

void shanchu(struct Tongxunlu *p,int *n)
{
    xianshi(p,*n);
    char name[10];
    printf("请输入需要删除的姓名:");
    scanf("%s",name);
    int m = *n;
    while(m--)
    {
        if(strcmp(name,p->name)==0)
        {
            *n = *n-1;
            memmove(p, p+1, m*sizeof(struct Tongxunlu));
            break;
        }
        p++;
    }
}
2020-02-23 09:36
快速回复:用指针的方式对一个结构数组里的元素进行删除,再显示出来,不会了,谢 ...
数据加载中...
 
   



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

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