【原创】一起学C++ 之指针、数组、指针算术 ---------C++ primer plus(第6版)
C++ Primer Plus 第6版
指针和数组基本等价的原因在于指针算术!
一、指针
⑴整数变量+1后,其值将增加1;
⑵指针变量+1后,增加的量等于它指向的类型的字节数;
⑶C++将数组名解析为地址;
例如:如果系统对double使用8个字节存储,其数值将增加8,
如果系统对short使用2个字节存储,则指针值将增加2
- #include <iostream>
- int main()
- {
- using namespace std;
- double wages[]={10000.0,20000.0,30000.0};
- short stacks[]={,,};
- double *pw=wages; // 将 pw 声明为指向 double类型的指针,然后将它初始化为wages----wages数组中的第一个元素的地址
- short *ps=&stacks[];
- int a;//-----仅为保持dos界面
- cout<<"pw= "<<pw<<", *pw ="<<*pw<<endl;
- pw=pw+;
- cout<<"add 1to the pw pointer:\n";
- cout<<"pw= "<<pw<<", *pw = "<<*pw<<"\n\n";
- cout<<"ps= "<<ps<<", *ps = "<<*ps<<endl;
- ps=ps+; //指针+1 等价于 *(stacks+1)
- cout<<"add 1 to the ps pointer:\n";
- cout<<"ps ="<<ps<<", *ps = "<<*ps<<endl;
- cout<<"access two elements with array notation\n";
- cout<<"stack[0]= "<<stacks[]
- <<", stack[1] ="<<stacks[]<<endl;
- cout<<"access two elements with pointer notation\n";
- cout<<"*stacks = "<<*stacks
- <<", *(stacks+1) ="<<*(stacks+)<<endl;
- cout<<sizeof(wages)<<" = size of wages array\n";
- cout<<sizeof(pw)<<" size of pw pointer\n";
- cin>>a;//-----仅为保持dos界面
- return ;
- }
解释上述代码:
1、在多数情况下,C++将数组名解释为数组第一个元素的地址;
和所有数组一样wages也存在下面的等式:
wages = &wages[0] =address of first element of array
除了首地址可以这么简写,其他的都必须为 “&+变量名+索引[]”
2、stacks[1] 和 *(stacks +1) 是等价的:可以这么理解,stacks是数组名的第一个元素地址,stacks=&stacks[0]--->(stacks+1)=&stacks[0+1]
所以 *(stacks +1) 等价于 stacks[1]
*(stacks +2) 等价于 stacks[2]
结论:
① 使用数组表示法时:
arrayname[i](数组名) 等价于 *( arrayname + i )
② 使用指针表示法时:
pointername[i](指针名称) 等价于 *( pointername + i)
③2者使用时区别
pointername=pointername+1; //正确的
arrayname=arrayname+1; //不正确的
下面这个例子是:地址偏移,和地址对应的数值
- double wages[]={10000.0,20000.0,30000.0};
- double *pw=wages;
- double *pw1=&wages[];
- cout<<"pw= "<<pw<<", *pw ="<<*pw<<endl;
- cout<<"pw= "<<pw1<<", *pw ="<<*pw1<<endl;
- pw=pw+;
- cout<<"add 1to the pw pointer:\n";
- cout<<"pw= "<<pw<<", *pw = "<<*pw<<"\n\n";
二、数组
1、静态联编(静态数组)
使用数组声明来创建数组时,将采用静态联编,即数组的长度在编译时设置:
- int tacos[];//static binding,size fixed at complie time
2、动态联编(动态数组)
使用 new[]来创建数组时,将采用动态联编(动态数组),即将在运行时维数组分配空间,其长度也将在运行时设置。使用完这种数组后,应使用delete[]来释放其占用的内存:
- int size;
- cin>>size;
- int * pz=new int [size]; //dynamic binding,size set at run time
- ...
- delete [] pz; //free memony when finished
三、指针算术
C++允许将指针和整数相加。加1的结果等于原来的地址值加上指向的对象占用的总字节数。
仅当2个指针指向同一个数组时,这种运算才有意义:这将得到2各元素的间隔。
- int tacos[]={,,,,,,,,,};
- int * pt =tacos;
- pt= pt + ;
- int * pe=&tacos[];
- pe=pe - ;
- int diff=pe-pt; // 结果:7
【原创】一起学C++ 之指针、数组、指针算术 ---------C++ primer plus(第6版)的更多相关文章
- C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | IT宅.com
原文:C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | IT宅.com C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | I ...
- C语言 指针数组指针
指向指针数组的指针. 1 #include <stdio.h> 2 3 int main(void) 4 { 5 char *pa[4]={"aaaaa"," ...
- 指针数组 vs 数组指针
指针数组,故名思义,就是指针的数组,数组的元素是指针: 数组指针,同样,就是直想数组的指针. 简单举例说明: int *p[2]; 首先声明了一个数组,数组的元素是in ...
- C语言->实验室->指针数组
一 分析 讨论指针数组要从三个层面来考虑: 1)指针数组本身是什么 2)指针数组作为参数时的表现 3)指针数组作为返回值时的表现 二 指针数组是什么 1)指针数组--指针的集合 数组是若干元素的集合, ...
- 指针数组 数组指针的区别.xml
pre{ line-height:1; color:#1e1e1e; background-color:#f0f0f0; font-size:16px;}.sysFunc{color:#627cf6; ...
- 指针数组与带参main函数
(一)指针数组 指针数组就是每一个元素存放一个地址,相当于一个指针变量.如:int *p[4]指针数组比较适合用来指向若干字符串,使得处理字符串更加灵活.例如,现在要将若干字符串按字母顺序由小到大输出 ...
- 【嵌入式开发】C语言 指针数组 多维数组
. 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21402047 . 1. 地址算数运算示例 指针算数运算 ...
- (C/C++)区别:数组与指针,指针与引用
1.数组跟指针的区别 数组要么在静态存储区被创建(如全局数组),要么在栈上被创建.数组名对应着(而不是指向)一块内存,其地址与容量在生命期内保持不变,只有数组的内容可以改变. 指针可以随时指向任意类型 ...
- C语言 指针数组 多维数组
. 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21402047 . 1. 地址算数运算示例 指针算数运算 ...
随机推荐
- 给jdk写注释系列之jdk1.6容器(2)-LinkedList源码解析
LinkedList是基于链表结构的一种List,在分析LinkedList源码前有必要对链表结构进行说明. 1.链表的概念 链表是由一系列非连续的节点组成的存储结构,简单分下类的话,链 ...
- [转]javascript js cookie的存储,获取和删除
本文转自:http://www.jb51.net/article/13240.htm 使用方法: //1.存储Cookie //2.参数说明: 1.参数1:Cookie存储Name,参数2:Cooki ...
- 【模拟】UVa 12108 - Extraordinarily Tired Students
When a student is too tired, he can't help sleeping in class, even if his favorite teacher is right ...
- truncate 函数用法示例
--Oracle trunc()函数的用法 /**************日期********************/ select trunc(sysdate) from dual --2015- ...
- Angular 2.0 从0到1:Rx--隐藏在Angular 2.x中利剑
第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...
- JavaScript “\”替换成 “\\”
str=str.replace(/\\/g,'\\\\');
- Java Concurrency - ScheduledThreadPoolExecutor
The Executor framework provides the ThreadPoolExecutor class to execute Callable and Runnable tasks ...
- 搜索本地网络内所有可用的SQl实例
'搜索本地网络内所有可用的SQl实例 Dim instance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance Dim dt ...
- 创建Mysql 序列
create table sequence( name ) not null primary key, current_value , increment , max_value BIGINT, -- ...
- IOS-UI-UIDynamic(一)
UIDynamic是从iOS7开始引入的技术 属于UIkit框架 可以模拟显示生活中的物理现象 如碰撞 抖动 摆动等 一. 使用UIDynamic步骤: 1.创建一个动力效果器UIDynamicAni ...