4.23 类型组合指针

2020-01-15  本文已影响0人  壹顾倾城

程序来源 :C++ primer plus
章 节 :4.23
名 称 :mixtypes.cpp
功 能 : 类型组合指针
开发时间 :2020-1-9
版 本 :v1.0
运行测试 :通过
C++11支持:执行工具-编译选项 输入
-std=c++11 并打钩

/********************************
 * 程序来源 :C++ primer plus
 * 章    节 :4.23
 * 名    称 :mixtypes.cpp
 * 功    能 : 类型组合指针 
 * 开发时间 :2020-1-9
 * 版    本 :v1.0
 * 运行测试 :通过
 * C++11支持:执行工具-编译选项 输入
 *            -std=c++11 并打钩
 *******************************/
#include <iostream>

using namespace std;

struct antarctica_years_end {
    int year;
    //some really interesting data, etc
};

//int main()
int main() {
    int year;
     
    antarctica_years_end s01, s02, s03;
    s01.year = 1998;
    
    antarctica_years_end *pa = &s02;   //pointer
    pa->year = 1999;
    
    antarctica_years_end trio[3] = {2000,2001,2002};   //array struct
    trio[0].year = 2000;
    
    const antarctica_years_end *arp[3] = {&s01, &s02, &s03};   //pointer array
    const antarctica_years_end **ppa = arp;                    //pointer to struct pointer
    auto ppb = ppa;    //c++11 automatic type deduction
    
    cout << "s01.year = " << s01.year << endl;
    cout << "pa->year = " << pa->year << ",address is:" << (int*)pa <<endl;
    cout << "trio.year = " << trio->year << endl;
    cout << "arp[1]->year = " << arp[1]->year << endl;
    cout << "(*ppa)->year = " << (*ppa)->year << endl;
    cout << "(*(ppb+1))->year = " << (*(ppb+1))->year << endl;
     
    return 0;
}

运行结果:

s01.year = 1998
pa->year = 1999,address is:0x6ffde0
trio.year = 2000
arp[1]->year = 1999
(*ppa)->year = 1998
(*(ppb+1))->year = 1999

--------------------------------
Process exited after 1.176 seconds with return value 0
请按任意键继续. . .
上一篇 下一篇

猜你喜欢

热点阅读