程序如下:
8 #include<iostream>
9 #include <unistd.h>
10 using namespace std;
11
12
13 int main(void)
14 {
15 unsigned int *t1 = new unsigned int[10];
16 unsigned int t2[3]={1,2,3};
17 unsigned int *t3;
18 short *t4 = new short;
19
20 t3=t2;
21 cout<< "dymic array szie is p t1:"<<sizeof(t1)<<endl;//不能对动态素组进行 他返回是指针的大小
22 cout<< "dymic short szie is p t4:"<<sizeof(t4)<<endl;//不能对动态分配short进行sizeof 他返回是指针的大小
23 cout<< "static array size if p t2"<<sizeof(t2)<<endl;
24 cout<< "static array pointer to t2 is p3 "<<sizeof(t3)<<endl;//不能对指向静态指向的素组使用sizeof 他返回是指针的大小
25
26 delete t4;
27 delete [] t1;
28
29 unsigned int (*t5)[3];
30 t5 = &t2;
31 t2+1; //step is 4
32 t5+1; //step is 4*3=12
33
34 sleep(10);
35
36
37 }
我们知道C/C++中数组的名字是指向数组第一个元素的指针,确实如此,我们可以*(p+1)来获取下一个元素
考虑这里的
29 unsigned int (*t5)[3];
30 t5 = &t2;
31 t2+1; //step is 4
32 t5+1; //step is 4*3=12
unsigned int t2[3]={1,2,3}; 是一个素组,那么t2是数组的名称也是他的首地址
t5 = &t2;如果解释呢其实如果我们这里将t2作为素组的名称,那么&t2就是素组
的起始位置,他和t2视乎有相同的地址,但是
t2+1
t5+1
他们的步长不同前者是4 后者这是这个素组的长度4*3
那么我们如何声明t5这种类型的指针,起始就是 int (*t5)[3];
他表明t5是一个指向3个int元素数组的首地址的指针。
可以用GDB进行调试
(gdb) p t2+1
2 = (unsigned int (*)[3]) 0x7fffffffea10
(gdb) p t5+1
$3 = (unsigned int (*)[3]) 0x7fffffffea1c
(gdb)
是不是看得很清楚 &t2为 0x7fffffffea10 t2+1为 0x7fffffffea14 t5+1为 0x7fffffffea1c
8 #include<iostream>
9 #include <unistd.h>
10 using namespace std;
11
12
13 int main(void)
14 {
15 unsigned int *t1 = new unsigned int[10];
16 unsigned int t2[3]={1,2,3};
17 unsigned int *t3;
18 short *t4 = new short;
19
20 t3=t2;
21 cout<< "dymic array szie is p t1:"<<sizeof(t1)<<endl;//不能对动态素组进行 他返回是指针的大小
22 cout<< "dymic short szie is p t4:"<<sizeof(t4)<<endl;//不能对动态分配short进行sizeof 他返回是指针的大小
23 cout<< "static array size if p t2"<<sizeof(t2)<<endl;
24 cout<< "static array pointer to t2 is p3 "<<sizeof(t3)<<endl;//不能对指向静态指向的素组使用sizeof 他返回是指针的大小
25
26 delete t4;
27 delete [] t1;
28
29 unsigned int (*t5)[3];
30 t5 = &t2;
31 t2+1; //step is 4
32 t5+1; //step is 4*3=12
33
34 sleep(10);
35
36
37 }
我们知道C/C++中数组的名字是指向数组第一个元素的指针,确实如此,我们可以*(p+1)来获取下一个元素
考虑这里的
29 unsigned int (*t5)[3];
30 t5 = &t2;
31 t2+1; //step is 4
32 t5+1; //step is 4*3=12
unsigned int t2[3]={1,2,3}; 是一个素组,那么t2是数组的名称也是他的首地址
t5 = &t2;如果解释呢其实如果我们这里将t2作为素组的名称,那么&t2就是素组
的起始位置,他和t2视乎有相同的地址,但是
t2+1
t5+1
他们的步长不同前者是4 后者这是这个素组的长度4*3
那么我们如何声明t5这种类型的指针,起始就是 int (*t5)[3];
他表明t5是一个指向3个int元素数组的首地址的指针。
可以用GDB进行调试
(gdb) p t2+1
2 = (unsigned int (*)[3]) 0x7fffffffea10
(gdb) p t5+1
$3 = (unsigned int (*)[3]) 0x7fffffffea1c
(gdb)
是不是看得很清楚 &t2为 0x7fffffffea10 t2+1为 0x7fffffffea14 t5+1为 0x7fffffffea1c