(1)int* p[2] 是一个指向int型的指针数组,即:p是包含两个元素的指针数组,指针指向的是int型。
  可以这样来用:
  

C/C++ code?
1
2
3
4
5
6
7
8
9
10
11
#include <iostream.h>
  void main() {
  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;
  }


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

C/C++ code?
1
2
3
4
5
6
7
8
9
10
#include <iostream.h>
  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)其等价形式如下:
  

C/C++ code?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream.h>
  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;
  }

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

 

int _tmain(int argc, _TCHAR* argv[])
{
 	int a[5] = {1,2,3,4,5};
//	int *p = new int[5];
// 	int (*p)[5] = &p;
	int (*p)[5] = &a;
	//int (*p1)[5] = a;//int *不能初始化int (*)[5]的实体
	cout<<"sizeof(p[0]) = "<<sizeof(p[0])<<",sizeof(p) = "<<sizeof(p)<<endl;
	cout<<"sizeof(a) = "<<sizeof(a)<<",sizeof(a[0]) = "<<sizeof(a[0])<<endl;
}

 

 

posted on 2014-11-18 10:59  风云逸  阅读(390)  评论(0)    收藏  举报