您的位置:首页 > 其它

数组指针,二级指针

2015-09-29 22:17 337 查看
char* a[] = {"hello", "the", "world"}; //1
char** pa =a;                          //2
//pa++;                                //3
cout << pa <<endl;                     //4
cout << *pa <<endl;                    //5
cout<<**pa<<endl;                      //6


最近看到这段代码,复习数组指针知识,做个记录备忘。

1. a是一个数组,里边存了若干个char型指针,等同于char *p = “hello”,然后把p(等于char*)存到a里边以此类推存完后2个。

2. 数组a的首地址复制给pa。由于a是个char*类型,用pa存a的地址需用char**。

3. pa++相当于a[1]。

4. 输出是数组a的地址。

5. *pa是数组第一个值相当于pa[0];*(pa+1)相当于pa[1];

6. **pa相当于pa[0][0],字符串第一个字母。

char* a[] = {"hello", "the", "world"}; //1
char** pa =a;                          //2
cout<<a<<endl;
cout<<(&a)<<endl;                      //3
cout<<&a+1 <<endl;                     //4
cout<<"_______________________" <<endl;
cout<<pa<<endl;                        //5
cout<<pa+1 <<endl;                     //6
3,4 输出为

a

0x0015fb28

    [0]: 0x00db783c "hello"

    [1]: 0x00db7838 "the"

    [2]: 0x00db7830 "world"

&a

0x0015fb28

    [0]: 0x00db783c "hello"

    [1]: 0x00db7838 "the"

    [2]: 0x00db7830 "world"
&a+1                                                 //a中存储char*类型元素共三个,3*4占12个字节,&a指针将12个字节的存储单元看成一个整体,+1按照12个字节跳转

0x0015fb34                                     

    [0]: 0xcccccccc <错误的指针>

    [1]: 0xcccccccc <错误的指针>

    [2]: 0x0015fb48 ""

pa

0x0015fb28

pa+1

0x0015fb2c

pa的类型为char** 四个字节,所以与&a+1不同

水平有限,有问题大家一定要指出来,共同提高。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: