#include<iostream>
using namespace std;
int ia[3][4] =
{
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11}
};
int main()
{
cout << "sizeof(int): " << sizeof(int) << endl;
// sizeof(int): 4
// ---------------------ia[3][4]--------------------------
cout << "ia: " << ia << "\t";
cout << "sizeof(ia): " << sizeof(ia) << endl;
cout << "*ia: " << *ia << "\t";
cout << "sizeof(*ia): " << sizeof(*ia) << endl;
cout << "&ia: " << &ia << "\t";
cout << "sizeof(&ia): " << sizeof(&ia) << endl;
cout << "ia[2]: " << ia[2] << "\t";
cout << "sizeof(ia[2]): " << sizeof(ia[2]) << endl;
cout << "*ia[2]: " << *ia[2] << "\t";
cout << "sizeof(*ia[2]): " << sizeof(*ia[2]) << endl;
cout << "&ia[2]: " << &ia[2] << "\t";
cout << "sizeof(&ia[2]): " << sizeof(&ia[2]) << endl;
cout << endl;
/*------------------初始化----------------
sizeof(int): 4
ia:
0x46e000
sizeof(ia): 48
*ia:
0x46e000
sizeof(*ia): 16
&ia:
0x46e000
sizeof(&ia): 4
ia[2]:
0x46e020
sizeof(ia[2]): 16
*ia[2]:
8
sizeof(*ia[2]): 4
&ia[2]:
0x46e020
sizeof(&ia[2]): 4
*/
// ---------------------(*ip)[4] = ia--------------------------
cout << endl;
int (*ip)[4] = ia; //(数组)变量的名字其实是指针,因此你需在标识符前加上*。
cout << "ip: " << ip << "\t";
cout << "sizeof(ip): " << sizeof(ip) << endl;
cout << "*ip: " << *ip << "\t";
cout << "sizeof(*ip): " << sizeof(*ip) << endl;
cout << "&ip: " << &ip << "\t";
cout << "sizeof(&ip): " << sizeof(&ip) << endl;
cout << "ip[3]: " << ip[3] << "\t";
cout << "sizeof(ip[3]): " << sizeof(ip[3]) << endl;
cout << "*ip[3]: " << *ip[3] << "\t";
cout << "sizeof(*ip[3]): " << sizeof(*ip[3]) << endl;
cout << "&ip[3]: " << &ip[3] << "\t";
cout << "sizeof(&ip[3]): " << sizeof(&ip[3]) << endl;
cout << endl;
/*------------------初始化----------------
ip:
0x46e000
sizeof(ip): 4
*ip:
0x46e000
sizeof(*ip): 16
&ip:
0x22fef4
sizeof(&ip): 4
ip[3]:
0x46e030
sizeof(ip[3]): 16
*ip[3]:
0
sizeof(*ip[3]): 4
&ip[3]:
0x46e030
sizeof(&ip[3]): 4
*/
// ---------------------(*ip1)[4] = ia; *ip2 = *ia;--------------------------
int (*ip1)[4] = ia; //(数组)变量的名字其实是指针,因此你需在标识符前加上*。
//ip1是等效于ia的二维数组。
int *ip2 = *ia; //ip2是指向二维数组ia第一内层数组的一维数组(unary array),
//即与ia[0][4]={0, 1, 2, 3},相同,ip[4]={0, 1, 2, 3}
cout << "ip1: " << ip1 << "\t";
cout << "sizeof(ip1): " << sizeof(ip1) << endl;
cout << "ip2: " << ip2 << "\t";
cout << "sizeof(ip2): " << sizeof(ip2) << endl;
cout << endl;
cout << "*ip1: " << *ip1 << "\t";
cout << "sizeof(*ip1): " << sizeof(*ip1) << endl;
cout << "*ip2: " << *ip2 << "\t";
cout << "sizeof(*ip2): " << sizeof(*ip2) << endl;
cout << endl;
cout << "*(ip1 + 1): " << *(ip1 + 1) << "\t";
cout << "ip1[1]: " << ip1[1] << endl;
cout << "*(ip2 + 1): " << *(ip2 + 1) << "\t";
cout << "ip2[1]: " << ip2[1] << endl;
cout << "**(ip1 + 1): " << **(ip1 + 1) << "\t";
cout << "ip1[1][0]: " << ip1[1][0] << endl;
// cout << "**(ip2 + 1): " << **(ip2 + 1) << "\t"; //ip2是一维数组
// cout << "ip2[2][0]: " << ip2[2][0] << endl; //ip2是一维数组
cout << "(*(ip1 + 1))[1]: " << (*(ip1 + 1))[1] << "\t";
cout << "ip1[1][1]: " << ip1[1][1] << endl;
//cout << "(*(ip2 + 1))[1]: " << (*(ip2 + 1))[1] << "\t"; //ip2是一维数组
//cout << "ip2[2][1]: " << ip2[2][1] << endl; //ip2是一维数组
cout << endl;
/* /*------------------初始化----------------
ip1: 0x46e000 sizeof(ip1): 4
ip2: 0x46e000 sizeof(ip2): 4
*ip1: 0x46e000 sizeof(*ip1): 16
*ip2: 0
sizeof(*ip2): 4
*(ip1 + 1):
0x46e010
p1[1]: 0x46e010
*(ip2 + 1):
1
ip2[1]: 1
**(ip1 + 1):
4
ip1[1][0]: 4
(*(ip1 + 1))[1]:
5
ip1[1][1]: 5
*/
// ---------------------ip = &ia[2];--------------------------
ip = &ia[2];
cout << "ip: " << ip << "\t"; //输出为:0x22fefc
cout << "sizeof(ip): " << sizeof(ip) << endl;
cout << "*ip: " << *ip << "\t"; //输出为:0x22fefc
cout << "sizeof(*ip): " << sizeof(*ip) << endl;
cout << endl;
/*------------------初始化----------------
ip: 0x46e020 sizeof(ip): 4
*ip: 0x46e020 sizeof(*ip): 16
*/
// ---------------------typedef--------------------------
cout << "typedef : " << endl;
typedef int int_array[4];
int_array *p = ia;
for(int_array *p = ia; p != ia + 3; ++p)
for(int *q = *p, i = 1; q != *p + 4; ++q, ++i)
{
cout << *q << "\t";
if(i % 4 == 0)
cout << endl;
}
cout << endl;
/* /*--------------------初始化----------------
typedef :
0 1 2 3
4 5 6 7
8 9 10 11
*/
return 0;
}
/*结果分析:
------------------------ia[3][4]----------------------------
sizeof(int): 4
ia:
0x46e000
sizeof(ia): 48
*ia:
0x46e000
sizeof(*ia): 16
&ia:
0x46e000
sizeof(&ia): 4
ia[2]:
0x46e020
sizeof(ia[2]): 16
*ia[2]:
8
sizeof(*ia[2]): 4
&ia[2]:
0x46e020 sizeof(&ia[2]): 4
-----------------------(*ip)[4] = ia;-----------------------
ip:
0x46e000
sizeof(ip): 4
*ip:
0x46e000
sizeof(*ip): 16
&ip:
0x22fef4
sizeof(&ip): 4
ip[3]:
0x46e030
sizeof(ip[3]): 16
*ip[3]:
0
sizeof(*ip[3]): 4
&ip[3]: 0x46e030 sizeof(&ip[3]): 4
------------------------(*ip1)[4] = ia; -------------------
------------------------*ip2 = *ia-------------------------
ip1:
0x46e000 sizeof(ip1): 4
ip2:
0x46e000 sizeof(ip2): 4
*ip1:
0x46e000
sizeof(*ip1): 16
*ip2:
0
sizeof(*ip2): 4
*(ip1 + 1):
0x46e010
ip1[1]: 0x46e010
*(ip2 + 1):
1
ip2[1]: 1
**(ip1 + 1):
4
p1[1][0]: 4
(*(ip1 + 1))[1]:
5
ip1[1][1]: 5
--------------------------ip = &ia[2];-----------------------------
ip: 0x46e020 sizeof(ip): 4
*ip: 0x46e020 sizeof(*ip): 16
----------------------------typedef--------------------------------
typedef :
0 1 2 3
4 5 6 7
8 9 10 11
Process returned 0 (0x0) execution time : 3.861 s
Press any key to continue.
*/
如果您对本文满意,请回复,谢谢^ . ^ . ^ . ^
同时欢迎提出不同意见,以帮助我改进,不胜感激!!!
——桑海整理