指针变量的大小 [英] size of pointer variables

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

问题描述

在特定架构上,不同类型的指针变量的大小可能是不同的。

例如。


sizeof(char *)是否可以与sizeof(int *)或

sizeof(void *)不同?


使用a的目的是什么无效指针?而不是

声明一个指针变量void *,我可以将它声明为

作为char *然后根据需要将它打印到任何类型的




提前任何帮助提供的任何帮助......

Can the size of pointer variables of different type may be
different on a particular architecture.
For eg.

Can the sizeof (char *) be different from sizeof(int *) or
sizeof (void *) ?

What is the purpose of using a void pointer ? Instead of
declaring a pointer variable "void *", can I declare it
as "char *" and then later on typcast it to whatever type
as needed.

Thanx for any any help in advance ...

推荐答案

嗨。


理论上,char *和int *的大小可以不同。我自己从来没有见过

,而且我认为这种情况并不常见。 void *

的目的是防止这个问题。你可以随时保证无效*将
适合任何东西,并且可以取任何指针的价值。


-

bjrnove

Hi.

In theory the size of a char* and a int* can be different. I never seen
it myself, and I don''t think it''s very common. The purpose of the void*
is to prevent this problem. You can always be shure that a void* will
fit into anything, and can take the value of any pointer.

--
bjrnove


< ju ********** @ yahoo.co.in>在消息中写道

news:11 ********************** @ g43g2000cwa.googlegr oups.com ...
<ju**********@yahoo.co.in> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
在特定架构上,不同类型的指针变量的大小是否可能不同。
例如。

sizeof(char *)可以与sizeof(int *)或
sizeof(void *)?
char *和int *可以不同,虽然我从未使用过

这样的系统。

例如:

A平台可以有32位或64位字作为最小的

可寻址单元,但选择支持8位字符,

存储额外的''字节偏移' '以及地址。


我不能想到char *和void *有不同的大小,

但我不记得是否/如何特别是ISO标准

要求它们必须具有相同的大小。

使用void指针的目的是什么?我可以将它声明为char *而不是声明指针变量void *,而不是声明指针变量void *。然后根据需要打印到任何类型

有时候需要使用char *以便能够执行某些指针算法(在特定于平台的代码中)。

实际上,''无符号char *''是首选,因为它还可以用
来复制原始内存。


这就是说,void *更好地表达了这样一个事实:指针

指的是可能有任何类型的对象的地址。

Thanx提前任何帮助......
Can the size of pointer variables of different type may be
different on a particular architecture.
For eg.

Can the sizeof (char *) be different from sizeof(int *) or
sizeof (void *) ? char* and int* can be different, although I have never used
such a system.
For example:
A platform could have 32 or 64-bit words as the smallest
addressable unit, yet choose to support 8-bit char-s by
storing an extra ''byte offset'' along with the address.

I can''t think of char* and void* having different sizes,
but I do not remember if/how the ISO standard specifically
mandates that they must have the same size.
What is the purpose of using a void pointer ? Instead of
declaring a pointer variable "void *", can I declare it
as "char *" and then later on typcast it to whatever type
as needed. Sometimes the use of char* is required anyway to be able to
perform some pointer arithmetic (in platform-specific code).
Actually, ''unsigned char*'' is to be preferred, as it
additionally can be used to copy raw memory.

This said, void* better expresses the fact that the pointer
refers to the address of an object that may have any type.
Thanx for any any help in advance ...




hth-Ivan

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格



hth-Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


" Ivan Vecerina < IN ********** @ vecerina.com>写道:
"Ivan Vecerina" <IN*************************@vecerina.com> wrote:
< ju ********** @ yahoo.co.in>在消息中写道
新闻:11 ********************** @ g43g2000cwa.googlegr oups.com ...
<ju**********@yahoo.co.in> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
sizeof(char *)可以与sizeof(int *)或
sizeof(void *)不同吗? char *和int *可以不同,虽然我从来没有使用过这样的系统。
Can the sizeof (char *) be different from sizeof(int *) or
sizeof (void *) ? char* and int* can be different, although I have never used
such a system.



我不能想到char *和void *有不同的大小,
但我不记得ISO标准是否/如何具体要求它们必须具有相同的尺寸。


I can''t think of char* and void* having different sizes,
but I do not remember if/how the ISO standard specifically
mandates that they must have the same size.




是的。同样的表示。



Yes. The same representation, as well.

使用void指针的目的是什么?我可以将它声明为char *而不是声明指针变量void *,而不是声明指针变量void *。然后根据需要将它打印到任何类型
What is the purpose of using a void pointer ? Instead of
declaring a pointer variable "void *", can I declare it
as "char *" and then later on typcast it to whatever type
as needed.




使用void *,你不需要演员表,更清洁。


Richard



Using void *, you don''t need the cast, which is cleaner.

Richard


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

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