zoukankan      html  css  js  c++  java
  • C/C++结构体总结

    #include"iostream" 
    using  namespace  std; 
     
    struct  TestStruct 
    { 
        int iNumber;
        char  charArray[10];
        char  ch;
        double dNumber;
    10  };
    11  //【嵌套的结构体类型成员】
    12  struct Date 
    13  {
    14      int Day;
    15      int Month;
    16      int Year;
    17  } ;
    18  struct Person /*定义结构体*/
    19  {
    20      char Name[20];
    21      int  Age;
    22      struct Date Birthday; //嵌套Date结构体类型成员
    23  } ;
    24  //【结构体中的指针成员】
    25  struct Student{   
    26      char *name;   
    27      int score;   
    28      struct Student* next;   
    29  };    
    30   
    31  int main(int argc, char* argv[])
    32  {
    33      //结构体的大小【结构体对齐】
    34      //A: 结构体变量的首地址能够被其最宽基本类型成员的大小所整除
    35      //B: 结构体每个成员相对于结构体首地址的偏移量都是成员自身大小的整数倍,如有需要编译器会在成员之间加上填充字节
    36      //C:结构体的总大小为结构体最宽基本类型成员大小的整数倍
    37      cout<<"Size of TestStruct "<<sizeof(TestStruct)<<endl;
    38      cout<<"Size of Date "<<sizeof(Date)<<endl;
    39      cout<<"Size of Person "<<sizeof(Person)<<endl;
    40   
    41      //【结构体变量】
    42      TestStruct t ={5,"bcdefg",'a',1.1};
    43      cout<<"TestStruct ch:"<<t.ch<<" charArray:"<<t.charArray<<" dNumber:"<<t.dNumber<<" iNumber:"<<t.iNumber<<endl;
    44     
    45      //【结构体数组】
    46      TestStruct tt[3] ={{8,"bcdefg",'m',1.2},{6,"hijklm",'b',2.2},{7,"nopqrs",'c',3.3}};
    47      for (int i=0;i<3;i++)
    48      {
    49          cout<<"TestStruct["<<i<< "]ch:"<<tt[i].ch<<" charArray:"<<tt[i].charArray<<" dNumber:"<<tt[i].dNumber<<" iNumber:"<<tt[i].iNumber<<endl;
    50      }
    51      //【指针变量与结构体变量】
    52      //必须要给结构体指针变量赋予一个有效的结构体变量地址,才能正常操作结构体指针变量。
    53      //TestStruct *p =&t;否则将出现不可预知的问题。(*p).ch 等价于 p->ch
    54      TestStruct *p =&t;
    55      cout<<"TestStruct p->ch:"<<p->ch<<" p->charArray:"<<p->charArray<<" p->dNumber:"<<p->dNumber<<" p->iNumber:"<<p->iNumber<<endl;   
    56      //【指向结构体数组的指针】
    57      //pp的初值为tt,即指向第一个元素,则pp加1后pp就指向下一个元素的起始地址(即tt[1]得起始地址)。
    58      TestStruct *pp ;
    59      for(pp =tt;pp<tt+3;pp++)
    60      {
    61          cout<<"TestStruct pp->ch:"<<pp->ch<<" pp->charArray:"<<pp->charArray<<" pp->dNumber:"<<pp->dNumber<<" pp->iNumber:"<<pp->iNumber<<endl;
    62      }
    63      //【嵌套的结构体类型成员】
    64      //访问嵌套结构体Date的成员 per->Birthday.Year  等价于 (*per).Birthday.Year
    65      Person *per; 
    66      per = new Person; //per=(Person *)malloc(sizeof(Person));    
    67      cout<<"Input name,age,year,month,day"<<endl;
    68      cin>>per->Name>>per->Age>>per->Birthday.Year>>per->Birthday.Month>>per->Birthday.Day;
    69      cout<<"Name:"<<per->Name<<" Age:"<<per->Age<<" Birthday:"<<(*per).Birthday.Year<<"/"<<per->Birthday.Month<<"/"<<per->Birthday.Day<<endl;
    70      delete per; //free(per);
    71      //【结构体中的指针成员】
    72      Student stu,*Pstu;
    73      //结构体成员指针需要初始化 
    74      stu.name = new char;//stu.name = (char*)malloc(sizeof(char)); 
    75      strcpy(stu.name,"ddd");   
    76      stu.score = 99;      
    77      //结构体指针需要初始化
    78      Pstu = new Student;//Pstu = (struct Student*)malloc(sizeof(struct Student));  
    79      //构体指针的成员指针同样需要初始化
    80      Pstu->name = new char;//Pstu->name = (char*)malloc(sizeof(char));  
    81      stu.next  = Pstu;   
    82      strcpy(Pstu->name,"cccc");   
    83      Pstu->score = 88;   
    84      Pstu->next = NULL;   
    85      cout<<"stu.name: "<<stu.name<<" stu.score: "<<stu.score<<endl;
    86      cout<<"stu.next->name: "<<stu.next->name<<" stu.next->score: "<<stu.next->score<<endl;
    87      cout<<"Pstu->name: "<<Pstu->name<<" Pstu->score: "<<Pstu->score<<endl;
    88      delete Pstu;
    89   
    90      return 0;
    91   
    92  }
    93   
  • 相关阅读:
    为什么GNU grep如此之快?
    在百度nlp实习一个月时间里用到的shell操作
    InfoPi简介、试用链接、资料索引
    自定义worker的方法,及一例
    2017.1.9版给信息源新增:max_len、max_db字段
    手把手写一个html_json信息源
    定义信息源的一些示例(xml文件)
    启用https协议的方法
    安装第三方Python模块,增加InfoPi的健壮性
    更新日志(建议升级到2017.1.18a) && 更新程序的方法
  • 原文地址:https://www.cnblogs.com/zhengfa-af/p/8145038.html
Copyright © 2011-2022 走看看