int*p[ ] int(*p)[ ] 区别 浅析int*p[ ]与int(*p)[ ]的区别

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

int*p[ ] int(*p)[ ] 区别 浅析int*p[ ]与int(*p)[ ]的区别

  2021-03-18 我要评论
想了解浅析int*p[ ]与int(*p)[ ]的区别的相关内容吗,在本文为您仔细讲解int*p[ ] int(*p)[ ] 区别的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:int*p[],int(*p)[],区别,下面大家一起来学习吧。
举例说明:
1)int* p[2] 是一个指向int型的指针数组,即:p是包含两个元素的指针数组,指针指向的是int型。
可以这样来用:
复制代码 代码如下:

<SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">#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;
}</SPAN>

2)对于 int (*p)[2], 它相当于一个二维数组的用法,只是它是一个n行2列的数组,可以这样来用:
复制代码 代码如下:

<SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">#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;
}
}</SPAN>

注意:
(1)为行数确定、列数不确定,即为2*n型的。
(2)为n*2型的数组的指针用法,即行数不确定、列数确定。
对于(1)其等价形式如下:
复制代码 代码如下:

<SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">#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;
}</SPAN>

其实以上用法即这我们常用的动态二维数组的用法。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们