指针程序示例:

1.简单运算

//pointer.cpp

#include<iostream>

int main()

{

using namespace std;

int updates = 6;

int *p_updates;

p_updates = &updates;

//value

cout << "Value:updates = " << updates;

cout << ",*p_updates = " << *p_updates << endl;

//address

cout << "Address: &updates= " << &updates;

cout << ",p_update= " << p_updates;

cout << ",&p_updates= " << &p_updates;

//use pointer to change value:

*p_updates += 1;

cout << "Now updates= " << updates<<endl;

cout << *p_updates;

cin.get();

return 0;

}

注意:在*p_updates+=1时,本来想用*p_updates++替换,结果只是地址加1,该指针指向的地址改变了,原因出在运算符的优先运算级别。

应改为(*p_updates)++!!!

2.指针和new初级

//use_new.cpp

#include<iostream>

int main()

{

using namespace std;

int nights = 1001;

int*  pt = new int;    //allocate space for an int

*pt = 1001;            //store a value here

cout << "nights value = "

<< nights << " : location = " << &nights << endl;

cout << "new int value = "

<< *pt << " : location = " << pt << endl;

double *pd = new double;

*pd = 1001.0;

cout << "double value = "

<< *pd << " : location = " << pd << endl;

cout << "location of pointer pd:" << &pd << endl;

cout <<"sizeof(pt):"<< sizeof(pt) << endl;

cout << "sizeof(*pt): " << sizeof(*pt) << endl;

cout << "sizeof(pt):" << sizeof(pd) << endl;

cout << "sizeof(*pt): " << sizeof(*pd) << endl;

cin.get();

return 0;

}

注意:该程序运行结果指出,new分配的内存块通常与常规变量分配的内存块不同。变量nights和pd的值都存储在被称为栈(stack)的内存区域内,而new从被称为堆(heap)或自由存储区的内存区域分配内存。

3.new和数组

//arraynew.cpp

#include<iostream>

int main()

{

using namespace std;

double *p3 = new double[3];     //space for 3 doubles

p3[0] = 0.1;                     //treat p3 like an array name

p3[1] = 0.2;

p3[2] = 0.3;

cout << "p3[1] = " << p3[1] << endl;

p3 += 1;                         //increment the pointer

cout << "Now p3[0] = " << p3[0] << endl;

cout << " and the p3[1] = " << p3[1] << endl;

p3 = p3 - 1;                     //pointer back to the beginning

delete[]p3;                      //free the memory

cin.get();

return 0;

}

注意:指针最后必须指向原来的值,这样程序可以给delete[]提供正确的地址。

3.指针和字符串

//ptrstr.cpp--uaing pointers to strings

#include<iostream>

#include<cstring>

int main()

{

using namespace std;

char animal[20] = "bear";

const char *bird = "wren";      //bird holds address of string

char *ps;

cout << animal << " and " << bird << endl;

cout << "Enter a kind of animal:";

cin >> animal;

ps = animal;

cout << ps << endl;

cout << "Before using strcpy():\n";

cout << animal << " at " << (int*)animal << endl;

cout << ps << " at " << (int*)ps << endl;

}

警告:在将字符串读入程序时,应使用已经分配的内存地址。该地址可以是数组名,也可以是使用new初始化的指针。

一般来说,如果给cout提供一个指针,它将打印地址。但如果指针的类型为char*,则cout将显示指向的字符串。如果要显示的是字符串的地址,则必须将这种指针强制转换为另一种指针类型,如int*。

4.使用new创建动态结构

//newstrct.cpp--using new with a struct

#include<iostream>

struct inflatable

{

char name[20];

float volume;

double price;

};

int main()

{

using namespace std;

inflatable *ps = new inflatable;  //allot memory for structure

cout << "Enter name of inflatable item: ";

cin.get(ps->name, 20);

cout << "Enter volume in cubic feet: ";

cin >> (*ps).volume;

cout << "Enter price: $";

cin >> ps->price;

cout << "Name: " << (*ps).name << endl;

cout << "Volume: " << ps->volume << endl;

cout << "Price: $: " << ps->price << endl;

delete ps;

cin.get();

cin.get();

return 0;

}

如果ps是指向结构的指针,则*ps就是被指向的值—结构本身。

5.一个使用new和delete的示例

//delete.cpp--using the delete operator

#pragma   warning(disable:4996)

#include<iostream>

#include<string.h>

using namespace std;

char* getname(void);  //function prototype

int main()

{

char* name;

name = getname();

cout << name << " at " << (int*)name << endl;

delete[]name;

name = getname();  //reuse freed memory

cout << name << " at " << (int*)name << endl;

delete[]name;

cin.get();   //只要有cin 多加一个cin.get

cin.get();

return 0;

}

char* getname()

{

char temp[80];

cout << "Enter last name: ";

cin>>temp;

char * pn = new char[strlen(temp) + 1];

strcpy(pn, temp);

return pn;

}

关于用[strlen(temp) + 1]

C-风格字符串具有一种特殊的性质:以空字符结尾(null character),空字符被写作\0。

6.类型组合

已经学习了数组、结构和指针,可以用各种方式组合它们。

//mixtypes.cpp--some type combinations

#include<iostream>

struct years

{

int year;

};

int main()

{

using std::cout;

years s0, s1, s2;

s0.year = 1998;

years *pa = &s1;

pa->year = 1999;

years tri[3];

tri[0].year = 2003;

cout << tri->year<<'\n';

const years *arp[3] = { &s0,&s1,&s2 };

cout << arp[1]->year << '\n';

const years **ppa = arp;

auto ppb = arp;     //C++11 automatic type deduction

//or use const years **ppb=arp

cout << (*ppa)->year << '\n';

cout << (*(ppb + 1))->year << std::endl;

std::cin.get();

return 0;

}

7.比较数组、vector对象和array对象

//choice.cpp--array variations

#include<iostream>

#include<vector>

#include<array>

int main()

{

using namespace std;

//C, original C++

double a1[4] = { 1.2,2.4,3.6,4.8 };

//C++98 STL

vector<double>a2(4);  //creat vector with 4 elements

//no simple way to initialize in C98

a2[0] = 1.0 / 3.0;

a2[1] = 1.0 / 5.0;

a2[2] = 1.0 / 7.0;

a2[3] = 1.0 / 9.0;

//C++11--create and initialize array object

array<double, 4>a3 = { 3.14,2.72 ,1.62,1.41 };

array<double, 4>a4;

a4 = a3;             //valid for array object of same size

//use array notation

cout << "a1[2]: " << a1[2] << " at " << &a1[2] << endl;

cout << "a2[2]: " << a2[2] << " at " << &a2[2] << endl;

cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;

cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;

//misdeed

a1[-2] = 20.2;

cout << "a1[-2]: " << a1[-2] << " at " << &a1[-2] << endl;

cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;

cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;

cin.get();

return 0;

}

三种都可以用标准数组表示法来访问各个元素。从返回地址可知,array对象和数组存储在相同的内存区域(即栈)中,而vector对象存储在另一个区域(自由存储区域或堆)中,第三,注意到可以将一个array对象赋给另一个array对象;而对于数组,必须逐个元素复制数据。

a1[-2] = 20.2;

索引-2表示:

*(a1-2) = 20.2 ;

找到a1所指定的地方,向前移动两个double元素,并将20.2存储到目的地址中。

学习《C++ Primer Plus》06 -2相关推荐

  1. (学习日记)2023.06.07

    写在前面: 由于时间的不足与学习的碎片化,写博客变得有些奢侈. 但是对于记录学习(忘了以后能快速复习)的渴望一天天变得强烈. 既然如此 不如以天为单位,以时间为顺序,仅仅将博客当做一个知识学习的目录, ...

  2. (学习日记)2023.06.06

    写在前面: 由于时间的不足与学习的碎片化,写博客变得有些奢侈. 但是对于记录学习(忘了以后能快速复习)的渴望一天天变得强烈. 既然如此 不如以天为单位,以时间为顺序,仅仅将博客当做一个知识学习的目录, ...

  3. vue3小兔鲜商城项目学习笔记+资料分享06

    建议大家先去看我第一篇小兔鲜的文章,强烈建议,非常建议,十分建议,从头开始看更完整. 最近正在学习vue3小兔鲜 下面是学习笔记 购物车模块 购物车功能分析 [外链图片转存失败,源站可能有防盗链机制, ...

  4. iOS学习笔记-091.彩票06——我的彩票

    彩票06我的彩票 一图示 二xib创建说明 三QWMMyLotteryViewControllerm 说明 彩票06--我的彩票 一.图示 二.xib创建说明 我们的界面可是使用 xib 来创建,创建 ...

  5. 如何学习C++ primer 第五版

    作者:dawnmist 链接:http://www.zhihu.com/question/32087709/answer/54936403 来源:知乎 著作权归作者所有,转载请联系作者获得授权. 作者 ...

  6. 学习C语言前必看!!(学习C Primer Plus笔记一)

    c语言的起源 1972年,贝尔实验室的丹尼斯·里奇和肯·汤普逊在开发UNIX操作系统时设计了C语言.但不是凭空想象的,在B语言(汤普逊发明)的基础上设计的. 虽然大多数语言都已实用为目标,单通常也考虑 ...

  7. SpringBoot学习之路:06.Spring Boot替换默认的Jackson

    2019独角兽企业重金招聘Python工程师标准>>> SpringBoot和Springmvc都可以返回接送数据,SpringBoot默认是使用Jackson解析json数据的,个 ...

  8. Sharepoin学习笔记—架构系列—06 Sharepoint服务(Services)与服务应用程序框架(Service Application Framework) 1

    Sharepoint服务是Sharepoint的重要组成,可以说Sharepoint的许多网站功能都是基于这些服务构架起来的.这里把Sharepoint服务的相关要点总结一下. 1.什么是 Share ...

  9. java学习总结(16.06.07)类的静态成员和非静态成员

    java里,类的成员可分为静态成员和非静态成员(实例成员),静态成员和非静态成员,从定义上来说就是有没有static修饰符修饰的区别.有static修饰的成员就是静态成员.如 public stati ...

  10. java学习总结(16.06.03)java中数组的定义和初始化

    刚开始接触java的数组时,我觉得java的数组和c++的数组虽然大致上差不多但细节上差很多,我也因此差点混乱了.后来自己仔细理了一下发现java和c++的数组只在定义和初始化上存在几点差异,而其他部 ...

最新文章

  1. PandaRSS 自助服务系统安装配置
  2. 【Linux网络编程】网络字节序和地址转换
  3. 休眠事实:有利于双向集vs列表
  4. js正整数正则表达式
  5. 论文浅尝 | 基于知识库的自然语言理解 03#
  6. 排序算法:冒泡排序算法优化实现及分析
  7. 笔记16(shell编程)
  8. IT 趣味故事:TCP 出“大事”了!
  9. 计算机应用设计的目的意义,高等教育自学考试计算机及应用专业+本科毕业设计(论文)的目的与要求...
  10. 91卫图助手下载器永久免费啦!
  11. 最小二乘支持向量机(基于MATLAB)
  12. Java实现 N的阶乘
  13. vim可视模式下复制粘贴文本
  14. 不同架构cpu上的c语言编译器,关于c ++:检测CPU架构的编译时
  15. java中怎么计算一个方法执行时,耗费多少毫秒
  16. element-ui实现表格分页和搜索功能
  17. JAVA中的String[] args和String args[]详解。
  18. 新媒体如何借势进行热点营销
  19. 首届博华深圳联展将于12月14日-16日在深圳国际会展中心举办
  20. [导入]n73手机拼音输入法

热门文章

  1. 可能是自己把你还当作高中的你,所以才哭了
  2. 量化投资_TB交易开拓者A函数和Q函数详解
  3. aarch64-linux-gnu 交叉编译 libpcap
  4. 春联编写最后打包制作成小程序
  5. 关于时间英文汇总和换算: millisecond, microsecond, nanosecond, picosecond
  6. Selenium中ExpectedConditions用法大全
  7. 哈尔滨理工大学软件与微电子学院程序设计竞赛(同步赛) A
  8. allegro转pads(使用allegro 16.3和pads9.3.1)
  9. [Flutter] BoxShadow阴影详解(blurRadius与spreadRadius的区别)
  10. VMware推免费服务器版虚拟软件