源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-03-04 04:20 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:浅析int*p[ ]与int(*p)[ ]的区别
[b]举例说明: [/b]1)int* p[2] 是一个指向int型的指针数组,即:p是包含两个元素的指针数组,指针指向的是int型。 可以这样来用:
[u]复制代码[/u] 代码如下:
<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列的数组,可以这样来用:
[u]复制代码[/u] 代码如下:
<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>
[b]注意: [/b](1)为行数确定、列数不确定,即为2*n型的。 (2)为n*2型的数组的指针用法,即行数不确定、列数确定。 [b]对于(1)其等价形式如下: [/b]
[u]复制代码[/u] 代码如下:
<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>
其实以上用法即这我们常用的动态二维数组的用法。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部