• C语言基础09


    指向结构体变量的指针叫做结构体指针:

    typedef struct {

      int num;

      char name[30];    // char *name; 程序会崩溃,*name本身是指针,没有什么空间的概念,只是指向另一个空间,并且另一空间的值是常量,不能被修改.

      float score;

      char gender;

    } Student;

    Student stu ={21,"luoshuai",76.5,'m'};

    Student *s1= &stu; // 注意这里需要使用到&符号来获取结构变量的地址.

    // 结构体指针的写法

    printf("%s ",s1->name);  //指针结构变量 可以使用指针名称-> 元素变量名

    printf("%s ",(*s1).name);

    //原来的结构体写法

    printf("%s ",stu.name);

    2.结构体数组与指针的关系:

    Student stu1 = {

      {21,"luoshuai",76.5,'m'},

      {12,"luoting",87.9,'w'},

      {25,"luoteng",95.7,'w'},

      {9,"luohuahua",68.8,'m'},

      {67,"liruoxuan",90.0,'w'}

    };

    Student *s2 = stu1;            // 注意这是是没有&

     printf("%s ",s2[1].name);        //输出luoting

    printf("%s ",(s2+1)->name);     //输出 luoting

    printf("%s ",(s2+4)->num );        // 67

    3.结构指针作为函数参数使用

    void printStudent( Student *stu, int count){   //注意这里传入*stu不是*stu[ ]

      for(int i = 0 ;i < count ;i ++){

        printf("学号:%d 姓名:%s ",(stu+i)->num,(stu+i)->name);

      }

  • 相关阅读:
    20220501 08:00:01
    20220508 08:00:01
    20220505 08:00:01
    20220430 08:00:01
    20220509 08:00:01
    20220507 08:00:01
    20220503 08:00:01
    20220506 08:00:01
    substrate学习编写简单pallet的问题记录
    Rust内存Layout
  • 原文地址:https://www.cnblogs.com/liruoxuan/p/4081930.html
Copyright © 2020-2023  润新知