1.学习过程中踩到两个坑。
第一个就是 *pstudinfo;
通过typedef 定义的指针类型 pstudinfo,在使用过程中不用重新添加 *号,它定义的变量就是指针变量,加 * 就会变成二重指针
第二个就是关于结构体中变量的调用,在调用结构体变量中的字符串的时候,不用加字符串的大小。 student[i].name
2.遍历结构体数组中的数据,使用的方法有两种
第一个是,通过指针遍历链表中的各个项。
(*pstu).name 和pstu ->sex是一样的,只不同的写法。
pstu->birth.year这个是调用结构体里面的结构体的写法。
pstu = pstu ->pLast; pstu = pstu ->pNext; 这两个操作是对指针进行更新,更新后指针指向的就是下一个地址。
第二个是,常规的通过结构体数组的下标遍历每个量。
#include "stdio.h"
typedef struct date
{
unsigned int year;
unsigned char mon;
unsigned char day;
};
typedef struct studinfo
{
unsigned char name[10];
unsigned char sex;
date birth;
studinfo *pNext;
studinfo *pLast;
}*pstuinfo;
int main()
{
unsigned char i;
pstuinfo pstu;
studinfo student[4] ={{ "zhao",'m' , {1992,9,14},0,0},
{ "qian",'f' , {1993,2,16},0,0},
{ "sun",'f' , {1992,3,11},0,0},
{ "lee",'m' , {1996,5,12},0,0},};
student[0].pNext = &student[1];
student[1].pNext = &student[2];
student[2].pNext = &student[3];
student[3].pLast = &student[2];
student[2].pLast = &student[1];
student[1].pLast = &student[0];
pstu = &student[0];
for(i = 0;i<4;i++)
{
printf("%s is %c born in %d-%d-%d \n\r",(*pstu).name,pstu ->sex,pstu->birth.year,pstu->birth.mon,pstu->birth.day);
pstu = pstu ->pNext;
}
pstu = &student[3];
for(i = 0;i<4;i++)
{
printf("%s is %c born in %d-%d-%d \n\r",(*pstu).name,pstu ->sex,pstu->birth.year,pstu->birth.mon,pstu->birth.day);
pstu = pstu ->pLast;
}
for(i = 0;i < 4;i ++)
{
printf(" %s is next to %s \n",student[i].name,student[i].pNext ->name);
}
}