c++指针加1

 int64_t *p2;
 char *p1;
 p1=(char*)0x800000;
 p2=(int64_t*)0x800000;
 char*a=p1+2;
 int64_t*b=p2+2;

那么a和b的值分别是:0x800002和0x800010

因为设指针p的类型为T,

则p+i=(p的值)+i*sizeof(T)的字节数

char是1个字节,int64_t是8个字节。

p1+2=(p1中存放的值)+2*1个字节数

p1中存放的值为16进制,则2字节也转为16进制。加起来是0x800002.

p2+2=(0x800000)+2*8字节=0x800010

指针中存放的地址为字节地址

 

char c1[]={'a','b','\0','d','e'};
    char c2[]="hello";
    char*c3="hello";
    cout << sizeof(c1) << endl;
    cout<<strlen(c1)<<endl;
    cout << sizeof(c2) << endl;
    cout << strlen(c2) << endl;
     cout << sizeof(c3) << endl;
    cout<<strlen(c3)<<endl;

猜你喜欢

转载自blog.csdn.net/weixin_36564655/article/details/79711815