zoukankan      html  css  js  c++  java
  • c++ 结构体

    struct Student               //声明一个结构体类型Student
    { int num;                 //包括一个整型变量num
      char name[20];           //包括一个字符数组name,可以容纳20个字符
      char sex;                //包括一个字符变量sex
      int age;                 //包括一个整型变量age 
      float score;             //包括一个单精度型变量
      char addr[30];           //包括一个字符数组addr,可以容纳30个字符
    };                        //最后有一个分号

    1、上面声明一个结构体:在内存中各占63个字节(4+20+1+4+4+30=63)

    2、结构体数组:

    struct Student                  
    { int num;
      char name[20];
      char sex;
      int age;
      float score;
      char addr[30];
    }sty[3]={{10101,″Li Lin″,′M′,18,87.5,″103 Beijing Road″},
    {10102,″Zhang Fun″,′M′,19,99,″130 Shanghai Road″},
    {10104,″Wang Min″,′F′,20,78.5,″1010,Zhongshan Road″}};

    3、指向结构体变量的指针:

      为了使用方便和使之直观,C++提供了指向结构体变量的运算符->,例如p->num表示指针p当前指向的结构体变量中的成员num。p->num 和(*p).num等价。同样,p->name等价于(*p).name。

      也就是说,以下3种形式等价:
        ① 结构体变量.成员名。如stu.num。
        ② (*p).成员名。如(*p).num。
        ③ p->成员名。如p->num。“->”称为指向运算符。
      请分析以下几种运算:
        p->n 得到p指向的结构体变量中的成员n的值。
        p->n++ 得到p指向的结构体变量中的成员n的值,用完该值后使它加1。
        ++p->n 得到p指向的结构体变量中的成员n的值,并使之加1,然后再使用它。

    #include <iostream>
    #include <string>
    using namespace std;
    int main( )
    {  
      struct Student //声明结构体类型student     {  int num;        string name;        char sex;        float score; };   Student stu; //定义Student类型的变量stu   Student *p=&stu; //定义p为指向Student类型数据的指针变量并指向stu   stu.num=10301; //对stu中的成员赋值   stu.name=″Wang Fun″; //对string变量可以直接赋值   stu.sex=′f′;   stu.score=89.5;   cot<<stu. num<<″ ″<<stu.name<<″ ″<<stu.sex<<″ ″<<stu.score<<endl;   cout<<(*p)>num<<″ ″<<(*p).name<<″ ″<<(*p).sex<<″ ″<<(*p).score<<endl;   return 0; }

    4、结构体生成链表:

      

    #define NULL 0   
    #include <iostream> 
    struct Student
    { long num;
      float score;
      struct Student *next;
    };
    int main( )
    {  Student a,b,c,*head,*p;
      a. num=31001; a.score=89.5;             //对结点a的num和score成员赋值
      b. num=31003; b.score=90;               //对结点b的num和score成员赋值
      c. num=31007; c.score=85;               //对结点c的num和score成员赋值
      head=&a;                                //将结点a的起始地址赋给头指针head
      a.next=&b;                              // a.next=&b;                              //将结点b的起始地址赋给a结点的next成员
      b.next=&c;                              //将结点c的起始地址赋给b结点的next成员
      c.next=NULL;                            //结点的next成员不存放其他结点地址
      p=head;                                 //使p指针指向a结点
      do        
      {  cout<<p->num<<″  ″<<p->score<<endl;     //输出p指向的结点的数据
         p=p->next;                              //使p指向下一个结点 
      } while(p!=NULL);                         //输出完c结点后p的值为NULL 
      return 0;
    }
  • 相关阅读:
    04-Bootstrap的插件
    03-Bootstrap学习
    02-移动端单位介绍
    01 响应式页面-@media介绍,
    14-jQuery补充
    13-jQuery的ajax
    12-事件委托(事件代理)
    11-jQuery的事件绑定和解绑
    10-事件对象
    09-JS的事件流的概念(重点)
  • 原文地址:https://www.cnblogs.com/lbangel/p/3268277.html
Copyright © 2011-2022 走看看