指针在哪里指向? [英] Where do pointers point to?

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

问题描述

是否有保证,指向C中任何对象的指针都指向该对象的最低寻址字节?


具体可能对于任何平台/ os /编译器组合:

(char *)v!=(void *)v

其中v是例如int变量。


如果是这样,任何现实生活中的例子?

解决方案

" x-pander" <纳克*** @ pitek.eu.org>写道:

是否有保证,指向C中任何对象的指针恰好指向该对象的最低寻址字节?


是的,据我所知。 (我很想听到聪明的

对此有不同意见,因为我会从中学习。)

特定于任何平台/ os /编译器都可以组合:
(char *)v!=(void *)v
其中v是一个int变量,例如。




non sequitur :你只是拖动整数到指针的转换,

这绝对是不便携的。

-

给我一个几年和一项大型研究补助金,

,我会给你一张收据。 - 理查德希思菲尔德


>具体来说,任何平台/ os /编译器组合都可以:

(char *)v!=(void *)v
其中v是一个int变量,例如。



CORRECTION,当然(!!!)我的意思是:

(char *)& v!=(void *)& v


在文章< cs *********** @ mamut.aster.pl>,

" x-迎合" <纳克*** @ pitek.eu.org>写道:

是否有保证,指向C中任何对象的指针恰好位于该对象的最低寻址字节?

具体是否有可能为任何平台/ os /编译器组合:
(char *)v!=(void *)v
其中v是一个int变量,例如。




一个指向整个对象的指针。


如果你把一个指针转换成另一个类型,让我们说从double *到int *,

你得到一个指向一个对象的指针,该对象从与

第一个对象相同的位置开始。因此,如果你指向char *的指针,它指向对象的第一个

字节。


你无法比较char *和void *。在你的例子中,编译器将

自动将(void *)v转换为char *,所以你实际上是比较

$

(char *)v ==(char *)(void *)v


保证相等。


Is is guaranteed, that a pointer to any object in C points exactly at the
lowest addressed byte of this object?

Specifcally is it possible for any platform/os/compiler combination that:
(char *)v != (void *)v
where v is an int variable for example.

If so, any real-life examples?

解决方案

"x-pander" <ng***@pitek.eu.org> writes:

Is is guaranteed, that a pointer to any object in C points exactly at the
lowest addressed byte of this object?
Yes, as far as I know. (I''d love to hear intelligent
disagreement on this, because I''d learn from it.)
Specifcally is it possible for any platform/os/compiler combination that:
(char *)v != (void *)v
where v is an int variable for example.



Non sequitur: you just dragged in integer-to-pointer conversion,
which is definitely non-portable.
--
"Give me a couple of years and a large research grant,
and I''ll give you a receipt." --Richard Heathfield


> Specifcally is it possible for any platform/os/compiler combination that:

(char *)v != (void *)v
where v is an int variable for example.



CORRECTION, of course (!!!) i meant:
(char *)&v != (void *)&v


In article <cs***********@mamut.aster.pl>,
"x-pander" <ng***@pitek.eu.org> wrote:

Is is guaranteed, that a pointer to any object in C points exactly at the
lowest addressed byte of this object?

Specifcally is it possible for any platform/os/compiler combination that:
(char *)v != (void *)v
where v is an int variable for example.



A pointer points to the whole object.

If you cast a pointer to another type, lets say from double* to int*,
you get a pointer to an object that starts at the same place as the
first object. So if you cast a pointer to char*, it points to the first
byte of the object.

You cannot compare char* and void*. In your example, the compiler will
convert the (void *) v automatically to char*, so you are actually
comparing

(char *)v == (char *) (void *) v

which is guaranteed to be equal.


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

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