void *指针加法

#include <iostream>
#include <stdio.h>
#include <stdint.h>

using namespace std;

int main(int argc, char **argv)
{
    uint32_t arr[3] = {0x01020304,1,2};
    void *p = arr;
    void *p1 = p + 2;
    cout<<p<<":"<<p1<<endl;
    cout<<hex<<*(int *)p<<endl;
    cout<<hex<<*(int *)p1<<endl;
    return 0;
}

编译:

test.cpp: In function 'int main(int, char**)':

test.cpp:12: warning: pointer of type 'void *' used in arithmetic

警告:使用void*指针加法

(gdb) print arr
$1 = {16909060, 1, 2}
(gdb) print p
$2 = (void *) 0x7fffffffe300
(gdb) print p1
$3 = (void *) 0x7fffffffe302
(gdb) print *p
Attempt to dereference a generic pointer.
(gdb) print *(char *)p
$4 = 4 '\004'
(gdb) print *(char *)p1
$5 = 2 '\002'
(gdb) print *(char *)(p1+1)
$6 = 1 '\001'
(gdb) print *(int *)p1
$7 = 65794
(gdb) x/10bx  0x7fffffffe300
0x7fffffffe300: 0x04    0x03    0x02    0x01    0x01    0x00    0x00    0x00
0x7fffffffe308: 0x02    0x00
(gdb) x/10bx
0x7fffffffe30a: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0xe3
0x7fffffffe312: 0xff    0xff

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值