int*p[ ]与int(*p)[ ]的区别
2015-09-19 21:43
274 查看
举例说明:
1)int* p[2] 是一个指向int型的指针数组,即:p是包含两个元素的指针数组,指针指向的是int型。
可以这样来用:
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int* p[2];
int a[3] = {1, 2, 3};
int b[4] = {4, 5, 6, 7};
p[0] = a;
p[1] = b;
for(int i = 0; i < 3; i++)
cout << *p[0] + i;// cout << **p + i;
cout << endl;
for(i = 0; i < 4; i++)
cout << *p[1] + i;// cout << **p + i;
return 0;
}
(2)对于 int (*p)[2], 它相当于一个二维数组的用法,只是它是一个n行2列的数组,可以这样来用:
#include <iostream>
using namespace std;
void main() {
int (*p)[2];
int b[3][2] = {{1, 2}, {3, 4}, {5, 6}};
p = b;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 2; j++) //cout << p[i][j]; //cout << *(*(p+i)+j);
cout << endl;
}
}
注意:
(1)为行数确定、列数不确定,即为2*n型的。
(2)为n*2型的数组的指针用法,即行数不确定、列数确定。
对于(1)其等价形式如下:
#include <iostream>
using namespace std;
void main() {
int** array;
array = new int* [2];
int a[3] = {1, 2, 3};
int b[4] = {4, 5, 6, 7};
array[0] = a; // *array = a;
array[1] = b; // *(array+1) = b;
for(int i = 0; i < 3; i++) cout << array[0][i];// cout << *array[0] + i;
cout << endl;
for(int j = 0; j < 4; j++) cout << array[1][j];// cout << *array[1] + j;
}
其实以上用法即这我们常用的动态二维数组的用法
1)int* p[2] 是一个指向int型的指针数组,即:p是包含两个元素的指针数组,指针指向的是int型。
可以这样来用:
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int* p[2];
int a[3] = {1, 2, 3};
int b[4] = {4, 5, 6, 7};
p[0] = a;
p[1] = b;
for(int i = 0; i < 3; i++)
cout << *p[0] + i;// cout << **p + i;
cout << endl;
for(i = 0; i < 4; i++)
cout << *p[1] + i;// cout << **p + i;
return 0;
}
(2)对于 int (*p)[2], 它相当于一个二维数组的用法,只是它是一个n行2列的数组,可以这样来用:
#include <iostream>
using namespace std;
void main() {
int (*p)[2];
int b[3][2] = {{1, 2}, {3, 4}, {5, 6}};
p = b;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 2; j++) //cout << p[i][j]; //cout << *(*(p+i)+j);
cout << endl;
}
}
注意:
(1)为行数确定、列数不确定,即为2*n型的。
(2)为n*2型的数组的指针用法,即行数不确定、列数确定。
对于(1)其等价形式如下:
#include <iostream>
using namespace std;
void main() {
int** array;
array = new int* [2];
int a[3] = {1, 2, 3};
int b[4] = {4, 5, 6, 7};
array[0] = a; // *array = a;
array[1] = b; // *(array+1) = b;
for(int i = 0; i < 3; i++) cout << array[0][i];// cout << *array[0] + i;
cout << endl;
for(int j = 0; j < 4; j++) cout << array[1][j];// cout << *array[1] + j;
}
其实以上用法即这我们常用的动态二维数组的用法
相关文章推荐
- 不要在初始化方法和dealloc方法中使用Accessor Methods
- PHP爬虫 爬取污染数据实例
- IO流demo--【J2SE】
- 条款37:绝不重新定义继承而来的缺省参数值(Never redefine a function's inherited default parameter value)
- Labeling Balls(拓扑排序wa)
- 个人日记-安卓里OnPageChangeListener的坑爹
- 超级删除PowerTools
- sublime 常用快捷键
- Minimum Cut(2015年吉林网络赛)
- hdu5459Jesus Is Here
- 黑马程序员之---C学习笔记之printf与scanf使用及注意事项
- 使用存取方法来设置Property value
- 测试用例的编写
- 傻瓜式操作Nagios图解
- 幸福的往事
- C语言_删除相同的字符(华为笔试题)
- 输入一个字符串,判断该字符串插入一个字符,能否使其成为回文字符串
- 求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字,例如:2+22+222+2222+22222
- 多路径路由算法选择(3)——路由器的简单介绍
- android性能测试内存泄漏