手机
当前位置:查字典教程网 >编程开发 >C语言 >浅析int*p[ ]与int(*p)[ ]的区别
浅析int*p[ ]与int(*p)[ ]的区别
摘要:举例说明:1)int*p[2]是一个指向int型的指针数组,即:p是包含两个元素的指针数组,指针指向的是int型。可以这样来用:复制代码代码...

举例说明:

1)int* p[2] 是一个指向int型的指针数组,即:p是包含两个元素的指针数组,指针指向的是int型。

可以这样来用:

复制代码 代码如下:

<SPAN>#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>#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>#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>

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

【浅析int*p[ ]与int(*p)[ ]的区别】相关文章:

short与int转换的小例子

c语言中getch,getche,getchar的区别

C++中抽象类和接口的区别介绍

浅析C语言中的sizeof

c++中typename和class的区别介绍

浅析iterator与指针的区别

枚举和宏的区别详细解析

char str[] 与 char *str的区别详细解析

exit和atexit的区别详细解析

C++中继承与组合的区别详细解析

精品推荐
分类导航