0

I'm doing a sample test question in C and can't explain the 'right answer'. The question is what is the output of:

#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;
}

And possible answers

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

I figured the answer was B (4 bytes + 1 byte) but they say the right answer is A (8 bytes). Their explanation is: Maximum size of the data type is 4 byte(int) in the structure.

Can someone perhaps explain the right answer better? Or is their right answer actually wrong.

TSG
  • 4,242
  • 9
  • 61
  • 121

0 Answers0