When executing this code on IDEONE:
#include <stdio.h>
#include <stdlib.h>
struct A{
int x;
char c;
};
struct B{
int y;
};
int main(void) {
// your code goes here
struct A* pa = malloc(sizeof(struct B));
printf("%d
",sizeof(*pa));
pa = malloc(sizeof(int));
printf("%d
",sizeof(*pa));
pa = malloc(sizeof(char));
printf("%d
",sizeof(*pa));
pa = malloc(0);
printf("%d
",sizeof(*pa));
return 0;
}
I got:
8
8
8
8
I'm guessing that since pa
is of type struct A *
and struct A
is 8 bytes long, then malloc is allocating 8 bytes, as it should, but if so, why use sizeof?