在C中添加unsigned long和int

时间:2016-04-16 19:20:50

标签: c hex unsigned-integer

    int meta_size = 24;
    node_t* prev;

    printf("%lx, ", prev + meta_size );
    printf("%lx, ", prev); 
    printf("%lx, ", meta_size);

输出:1519240,1519000,18 这是怎么回事?

2 个答案:

答案 0 :(得分:2)

Prev是一个单元化指针,它包含(一个随机的,因为它没有被初始化)内存地址。

printf("%lx, ", prev + meta_size );//Prints the memory address prev is pointing to + (sizeof(node_t) * meta_size)
printf("%lx, ", prev);             //Prints the memory address prev is pointing to
printf("%lx, ", meta_size);        //Prints meta_size, 18 is 24 in hexidecimal, because of the 'x' in %lx 

但是,前两行是未定义的行为,因为指针应使用%p

打印

答案 1 :(得分:0)

如果你做sizeof(node_t)我很确定你会获得10.