获取指针的大小用C [英] Get size of pointer in C

查看:104
本文介绍了获取指针的大小用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得一个指针的下使用尺寸的sizeof ?我想malloc的一些内存来存储一个指针(不是值所指向)。

How do I get the size of a pointer in C using sizeof? I want to malloc some memory to store a pointer (not the value being pointed to).

推荐答案

给定一个任意类型(我选择了字符在这里,但这是具体的例子的缘故):

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

char *p;

您可以使用这些前pressions的:

You can use either of these expressions:

sizeof(p)
sizeof(char *)

导致的malloc()调用,如:

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

最后一个版本有一些好处,如果 PPC类型的变化,前pression仍然分配正确的空间。

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

这篇关于获取指针的大小用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆