美文网首页
小甲鱼笔记#一个关于指针所占空间的问题

小甲鱼笔记#一个关于指针所占空间的问题

作者: mark_x | 来源:发表于2019-07-29 16:49 被阅读0次
    #include <stdio.h>
    
    int main()
    {
        char a = 'F';
        int b =123;
        
        char *pa = &a;
        int *pb = &b;
        
        printf("a = %c\n", *pa);
        printf("b = %d\n", *pb);
        
        *pa = 'C';
        *pb += 1;
        
        printf("now, a = %c\n", *pa);
        printf("now, b = %d\n", *pb);
        
        printf("The addr of a is %p\n", pa);
        printf("The addr of b is %p\n", pb);
        
        printf("The length of pa is %d\n", sizeof(pa));
        printf("The length of pb is %d\n", sizeof(pb));
        
        return 0;
    }
    

    输出为:

    a = F
    b = 123
    now, a = C
    now, b = 124
    The addr of a is 0060FF07
    The addr of b is 0060FF00
    The length of pa is 4
    The length of pb is 4
    

    既然指针所占内存空间为4个字节,为什么地址相差8个字节呢?

    相关文章

      网友评论

          本文标题:小甲鱼笔记#一个关于指针所占空间的问题

          本文链接:https://www.haomeiwen.com/subject/osgbrctx.html