美文网首页
结构体(二)

结构体(二)

作者: qianranow | 来源:发表于2018-04-26 15:36 被阅读40次

    0. 指针和结构体


    • 概念:通过 指针 保存 结构体变量地址
      struct Person {
        char *name;
        int age;
        double height;
      };
      struct Person sp = {"wxx", 18, 2.00};
      struct Person *sip = NULL;
      sip = &sp;
      
    • 操作
      struct Person {
      char *name;
      int age;
      double height;
      };
      struct Person sp = {"wxx", 18, 2.00};
      struct Person *sip = NULL;
      sip = &sp;
      
      (*sip).name = "qtt";
      (*sip). age = 18;
      (*sip). height = 1.75;
      printf("name = %s, age = %i, height = %lf\n", (*sip).name, (*sip). age, (*sip). height);
      
      sip->name = "xdy";
      sip->age = 1;
      sip->height = 0.75;
      printf("name = %s, age = %i, height = %lf\n", sip->name,  sip->age, sip->height);
      

    1. 结构体数组


    • 概念:数组元素结构体值
      struct Department {
        char *name;
        int count;
        double kpi;
      };
      struct Department departments[3] = {
      
        {"iOS", 10, 100.0},
        {"andorid", 10, 88.0},
        {"php", 100, 66.0}
      
      };
      departments[0].name = "秦子阳";
      departments[0].count = 1000;
      departments[0].kpi = 10000.0;
      printf("name = %s, count = %i, kpi = %lf\n", departments[0].name, departments[0].count, departments[0].kpi);
      

    2. 结构体嵌套


    • 概念:结构体属性 又是 一个结构体
      struct Time {
      
        int HH;
        int mm;
        int ss;
      
      };
      
      struct Data {
        
        int year;
        int month;
        int day;
        struct Time time;
        
      };
      
      struct Person {
        
        char *name;
        int age;
        struct Data work;
        
      };
      
      struct Person sp = {
        
        "wxx",
        18,
        {
          2016,
          10,
          10,
          {
            9,
            10,
            8
          }
        }
        
      };
      
      printf("year = %i, month = %i, day = %i\n", sp.work.year, sp.work.month, sp.work.day);
      

    3. 结构体和函数


    • 结构体类型变量 作为 形参,修改形参数据 不能改变 实参数据
      struct Person {
      
      char *name;
      int age;
      double height;
      
      };
      
      void demo(struct Person);
      
      int main(int argc, const char * argv[]) {
      
        struct Person sp = {"wx", 18, 2.00};
      
        demo(sp);
      
        printf("sp.age = %i\n", sp.age);
      
        return 0;
      
      }
      
      void demo(struct Person p) {
      
        p.age = 20;
      
        printf("p.age = %i\n", p.age);
      
      }
      
    • 结构体类型指针变量 作为 形参,修改形参数据 会改变 实参数据
      struct Person {
      
      char *name;
      int age;
      double height;
      
      };
      
      void demo(struct Person *);
      
      int main(int argc, const char * argv[]) {
      
        struct Person sp = {"wx", 18, 2.00};
      
        demo(&sp);
      
        printf("sp.age = %i\n", sp.age);
      
        return 0;
      
      }
      
      void demo(struct Person *p) {
      
        p->age = 20;
      
        printf("p.age = %i\n", p->age);
      
      }
      

    相关文章

      网友评论

          本文标题:结构体(二)

          本文链接:https://www.haomeiwen.com/subject/znlplftx.html