C中结构的大小与预期不符

时间:2017-03-02 02:06:13

标签: c struct sizeof

我在C中做一个示例测试问题并且无法解释正确答案'。问题是:

的输出是什么
#include<stdio.h>
#include<stdlib.h>
struct st{
    int a;
    char b;
};

int main()
{
    struct st *st_ptr;
    st_ptr = malloc(sizeof(struct st));
    printf("%d\n",sizeof(struct st));
    return 0;
}

可能的答案

a) 8
b) 5
c) 0
d) none of the mentioned

我认为答案是B(4字节+ 1字节),但他们说正确的答案是A(8字节)。他们的解释是:数据类型的最大大小为4字节(整数)。

有人可以更好地解释正确的答案吗?或者他们的正确答案实际上是错误的。

0 个答案:

没有答案
相关问题