Get size of pointer in C

Given an arbitrary type (I’ve chosen char here, but that is for sake of concrete example):

char *p;

You can use either of these expressions:

sizeof(p)
sizeof(char *)

Leading to a malloc() call such as:

char **ppc = malloc(sizeof(char *));
char **ppc = malloc(sizeof(p));
char **ppc = malloc(sizeof(*ppc));

The last version has some benefits in that if the type of ppc changes, the expression still allocates the correct space.

Leave a Comment