指针数组和数组指针的区别
2016-03-18 22:01
363 查看
指针数组:array of pointers,即用于存储指针的数组,也就是数组元素都是指针
数组指针:a pointer to an array,即指向数组的指针
还要注意的是他们用法的区别,下面举例说明。
int* a[4] 指针数组
表示:数组a中的元素都为int型指针
元素表示:*a[i] *(a[i])是一样的,因为[]优先级高于*
int (*a)[4] 数组指针
表示:指向数组a的指针
元素表示:(*a)[i]
注意:在实际应用中,对于指针数组,我们经常这样使用:
typedef int* pInt;
pInt a[4];这跟上面指针数组定义所表达的意思是一样的,只不过采取了类型变换。
#include <iostream>
using namespace std;
int main()
{
int c[4]={1,2,3,4};
int *a[4]; //指针数组
int (*b)[4]; //数组指针
b=&c;
//将数组c中元素赋给数组a
for(int i=0;i<4;i++)
{
a[i]=&c[i];
}
//输出看下结果
cout<<*a[1]<<endl; //输出2就对
cout<<(*b)[2]<<endl; //输出3就对
return 0;
}
数组指针:a pointer to an array,即指向数组的指针
还要注意的是他们用法的区别,下面举例说明。
int* a[4] 指针数组
表示:数组a中的元素都为int型指针
元素表示:*a[i] *(a[i])是一样的,因为[]优先级高于*
int (*a)[4] 数组指针
表示:指向数组a的指针
元素表示:(*a)[i]
注意:在实际应用中,对于指针数组,我们经常这样使用:
typedef int* pInt;
pInt a[4];这跟上面指针数组定义所表达的意思是一样的,只不过采取了类型变换。
#include <iostream>
using namespace std;
int main()
{
int c[4]={1,2,3,4};
int *a[4]; //指针数组
int (*b)[4]; //数组指针
b=&c;
//将数组c中元素赋给数组a
for(int i=0;i<4;i++)
{
a[i]=&c[i];
}
//输出看下结果
cout<<*a[1]<<endl; //输出2就对
cout<<(*b)[2]<<endl; //输出3就对
return 0;
}
相关文章推荐
- 程序在关闭时向Server发送信息
- js中特殊数值常量值
- Visual Studio 2015的安装及单元测试练习
- Oracle中SQL基本操作语句
- 最短路径算法之Dijkstra算法_Java实现
- openjudge 2757 最长上升子序列
- oracle的日期和时间的处理
- maximum subarray
- Python中特殊函数和表达式 filter,map,reduce,lambda
- java面向对象中四种权限(private,protected,public,友好型)详解
- 2016.03.18 (《All Right - 金艺林》)
- UISearchBar
- android开发步步为营之97:几种常用的android图片内存优化方法避免OOM
- 字节对齐
- openwrt之强制关掉应用程序
- 搭建Java环境
- 1015
- Uva1586
- 数据结构的学习(1)(c语言)
- 访问祖先类的虚方法(直接访问祖先类的VMT,但是这种方法在新版本中未必可靠)