标题:在结构体中还有结构体如何用指针访问?
只看楼主
dzy123
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:379
专家分:820
注 册:2013-4-18
结帖率:82%
 问题点数:0 回复次数:1 
在结构体中还有结构体如何用指针访问?
程序代码:
#include <stdio.h>
#include <stdlib.h>
struct date {
    int year;
    int month;
    int day;
};
struct student {
    char name[20];
    char sex[4];
    struct date birthday;
};
void Insert(struct student *,int );
void Display(struct student *,int);
int main(void) {
    struct student stu[20];
    Insert(stu,2);
    Display(stu,2);
    system("pause");
    return 0;
}
void Insert(struct student *stu,int n) {
    struct student *p;
    for(p=stu; p<stu+n; p++) {
        scanf("%s",p->name);
        scanf("%s",p->sex);
        scanf("%d",p->birthday->year);
        scanf("%d",p->birthday->month);
        scanf("%d",p->birthday->day);
    }
}
void Display(struct student *stu,int n) {
    struct student *p;
    for(p=stu; p<stu+n; p++) {
        printf("%s ",p->name);
        printf("%s ",p->sex);
        printf("%d-",p->birthday->year);
        printf("%d-",p->birthday->month);
        printf("%d",p->birthday->day);
        printf("\n");
    }
}

我这样访问是错误的有大神能指导下不
搜索更多相关主题的帖子: 结构体 struct int stu student 
2018-04-23 19:57
dzy123
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:379
专家分:820
注 册:2013-4-18
得分:0 
我自己试出来了谢谢各位
程序代码:
void Insert(struct student *stu,int n) {
    struct student *p;
    for(p=stu; p<stu+n; p++) {
        scanf("%s",p->name);
        scanf("%s",p->sex);
        scanf("%d",&p->birthday.year );
        scanf("%d",&p->birthday.month);
        scanf("%d",&p->birthday.day);
    }
}
void Display(struct student *stu,int n) {
    struct student *p;
    for(p=stu; p<stu+n; p++) {
        printf("%s ",p->name);
        printf("%s ",p->sex);
        printf("%d-",p->birthday.year);
        printf("%d-",p->birthday.month);
        printf("%d",p->birthday.day);
        printf("\n");
    }
}
2018-04-23 20:15



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-486319-1-1.html




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

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