学习笔记

C++PrimerPlus指针
Publish: 2018/7/23   

指针

指针是一个变量,其存储的是值得地址,而不是值本身。通过&name获取变量地址。

声明和初始化指针
使用new来分配内存
使用delete释放内存
使用new创建动态数组
指针、数组和指针算术
double wages[3]={1000.0,2000.0,3000,0};
double * pw =wages;
指针和字符串
使用new创建动态结构
struct inflatable
{
    float volume;
    double price;
};

inflatable * ps = new inflatable;
//通过ps->price或(*ps).price访问结构成员 
delete ps;

指针数组

struct antarctica
{
    int year;
};

创建类型变量
antarctica s01,s02,s03;
创建结构指针
antarctica * pa = &s01;
创建指针数组
const antarctica * arp[3]={&s01,&s02,&s03};
创建指向指针数组的指针
const antarctica ** ppa = arp;=auto ppb = arp;
通过(*(ppa+i))->year访问结构中的元素

数组的替代品vector和array

指针和const

int sloth=3;
const int * ps = &sloth;
int * const finger = &sloth;
// finger和*ps都是const,而*finger和ps不是
double trouble = 2.0E30;
const double * const stick = &trouble;
//stick和*stick都是const


← C++PrimerPlus函数 C++PrimerPlus复合类型 →

Powered by Hexo, Theme designs by @hpcslag.
Style-Framework Tocas-UI designs by @yamioldmel