Get size of pointer in C

安稳与你 提交于 2019-11-28 06:27:10

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.

This should do the trick:

sizeof(void*)
Gurpreet Singh
char *ptr;
char **ptr2 = malloc(sizeof(ptr));

should be able to achieve your purpose. No matter what the platform is, this code should work.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!