关于sizeof的几个问题

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main(int argc, char const *argv[])
 5 {
 6     char *pstr;
 7     int a[]={1,2,3,4,5,6};
 8     pstr=(char *)malloc(sizeof(char)*10);
 9     printf("%d\n", sizeof(pstr));
10     printf("%d\n", sizeof(a));
11     return 0;  
12 }  

运行结果:

 

 sizeof求的是字节数啊啊啊啊啊啊!!!!!!!!!!!!!!

 

 另:sizeof是一个关键字而不是函数

 


 

结构体字节对齐问题:

#include <stdio.h>

struct ts
{
    char c;
    int n;
};

int main(int argc, char const *argv[])
{
    struct ts t;
    printf("sizeof(t.c)=%d\n", sizeof(t.c));
    printf("sizeof(t.n)=%d\n", sizeof(t.n));
    printf("sizeof(t)=%d\n", sizeof(t));
    return 0;
}

 运行结果:

注意看sizeof(t)大小为8哦,说明成员c被补成4字节了

 

posted on 2017-09-26 19:25  枝桠  阅读(209)  评论(0编辑  收藏  举报

导航