What is the reason for seemingly inconsistent sizes of pointers and struct types?

Because, as sizeof(char *), sizeof(person) returns the size of a pointer. And in your case, a pointer to a struct (here to a Person struct) is of size 8.

And sizeof(person->name) returns the size of a pointer on a char as name is defined as a char *.

buffer is not a pointer, it is an array. The compiler knows it and sizeof(buffer) returns the size of the array, even through there is some similarities between the name of an array and a pointer, they are not treated the same.


For starters to output an object of the type size_t you shall use the conversion specifier zu

printf("sizeof(Person): %zu\n", sizeof(Person));
                        ^^^   

I don't understand the following: If sizeof(Person) == 16, why sizeof(person) == 8 and not 16?

The name Person denotes the structure

typedef struct person{
    char *name;
    int age;
}Person;

An object of this type occupies 16 bytes.

The name person declared like

Person *person = (Person*)malloc(sizeof(Person));

denotes a pointer. This pointer occupies 8 bytes and points to a memory allocated for an object of the type Person that occupies 16 bytes.

That is sizeof( Person ) and sizeof( Person * ) that is equivalent to sizeof( person ) are two different expressions.

And if sizeof(buffer) == 32, why sizeof(person->name) == 8 and not 32?

Again the name name has a pointer type and occupies 8 bytes. It is a data member of the structure person declared like

char *name;

This pointer points to a dynamically allocated memory that occupies 32 bytes.

Pay attention to that the size of a pointer does not depend on whether it points to a single object or to the first element of an array. That is you can allocate memory for a very big array but nevertheless the size of the pointer pointing to the allocated memory will not be changed depending on the size of the allocated memory.

Consider for example

int a[10];

int *p = a;

int b[10000];

int *q = b;

In this example the pointers p and q have the same size. You could write for example

int a[10];

int *p = a;

int b[10000];

p = b;

The size of the pointer p will not be changed after the last assignment.


As mentioned in a comment by PSkocik.

person is a pointer, same size as pointer to char. same for person->name.
struct person is a type, same size as Person.