美文网首页
4.23 类型组合指针

4.23 类型组合指针

作者: 壹顾倾城 | 来源:发表于2020-01-15 16:04 被阅读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
请按任意键继续. . .

相关文章

  • 4.23 类型组合指针

    程序来源 :C++ primer plus章 节 :4.23名 称 :mixtypes.cpp功 ...

  • 指针运算

    指针的类型与指针指向的类型明显不相等 指针的类型表示指针自身的类型; 指针指向类型表示指针指向数据(内存)的类型。...

  • 指针小解

    原文链接指针的类型分为指针本身的类型和指针所指向的类型 int *ptr; //指针的类型是int *, 指针...

  • Golang学习 - unsafe 包

    指针类型: *类型:普通指针,用于传递对象地址,不能进行指针运算。 unsafe.Pointer:通用指针类型,用...

  • 数据类型由浅到深

    基本类型 数组 指针 函数原型 函数指针 除了不能定义函数原型的数组外,其他的都可以相互组合,千变万化,尤其是函数...

  • C 语言指针

    指针类型:指针的读取长度,指针的读取方向(大小端) 空指针,无类型指针,野指针 常量指针,指向常量的指针 http...

  • unsafe包

    go语言的指针类型分为三种:(1)普通指针类型:*类型,用于存储地址,不能进行指针运算(2)通用指针类型:用于转换...

  • 指针

    指针是一种数据类型。指针装的是地址类型的数据。 注意:指针指向A的地址,那么:*+指针名=A的值!!! 指针类型 ...

  • 指针2

    指针三要素1正在自身类型, 指针指向的类型 ,指针指向的地址 数组指针 int(*p)[5] 指针数组 int ...

  • C++ 函数指针和函数类型

    函数指针和函数类型 函数指针指向的是函数而非对象。和其他指针类型一样,函数指针指向某种特定类型。 函数类型由它的返...

网友评论

      本文标题:4.23 类型组合指针

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