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 





轉自:  http://www.cnblogs.com/dyufei/archive/2010/11/29/2573906.html

發佈了24 篇原創文章 · 獲贊 38 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章