C语言学习-指针3

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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值