变量和指针.. noob问题.. [英] variables and pointers.. noob question..

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

问题描述

我在c ++学习新手......我有以下问题...

阅读一些源代码,我看到了:


int function(const void * one,const void * two)

{

int var1,var2;

var1 = *(( int *)one);

var2 = *((int *)two);

/ * sm其他代码在这里* /

}


我的问题是关于var1分配的rhs。 - *((int *)one);

i确定它的作用; P

我的问题是如何完成的...我的意思是,为什么这么多的解引用

运营商和如此多的括号?难道不是那么复杂吗?


能有人解释一下这是怎么回事吗?

tia,streamkid :)

i''m a learning newbie at c++... and i have the following question...
reading some source code, i saw this:

int function(const void * one, const void * two)
{
int var1, var2;
var1 = *((int*)one);
var2 = *((int*)two);
/* sm other code here*/
}

my question is about the rhs of var1 assigment. -*((int*)one);
i am sure about what it does ;P
my question is how this is done.. i mean, why so many dereference
operators and so many parentheses? isn''t that to complicated??

can someone explain how this works??
tia, streamkid :)

推荐答案

< st ******* @ gmail.comwrote in message

news:11 ****** ****************@i3g2000cwc.googlegro ups.com ...
<st*******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...

我在c ++学习新手......我有以下问题...

阅读一些源代码,我看到了这个:


int function(const void * one,const void * two)

{

int var1,var2;

var1 = *((int *)one);

var2 = *((int *)two);

/ * sm其他代码在这里* /

}


我的问题是关于var1分配的rhs。 - *((int *)one);

i确定它的作用; P

我的问题是如何完成的...我的意思是,为什么这么多的解引用

运营商和如此多的括号?难道不是那么复杂吗?


有人能解释一下这是怎么回事吗?

tia,streamkid :)
i''m a learning newbie at c++... and i have the following question...
reading some source code, i saw this:

int function(const void * one, const void * two)
{
int var1, var2;
var1 = *((int*)one);
var2 = *((int*)two);
/* sm other code here*/
}

my question is about the rhs of var1 assigment. -*((int*)one);
i am sure about what it does ;P
my question is how this is done.. i mean, why so many dereference
operators and so many parentheses? isn''t that to complicated??

can someone explain how this works??
tia, streamkid :)



嗯......


一个类型为const void *:我们不能解释那个(What''sa" void" ;?!

不,真的,不回答任何人......)


但是我们碰巧知道一个人真正指向一个int而我们想要得到

那个int。 嗯,我们说,如果我们只有一个类型为int *的指针,那么

int ...碰巧的是,我们可以通过编写一个来输入一个来输入int *,即通过写一个(int *)来获得
。但是这仍然没有给我们指向我们。对于

,我们必须取消引用这个新的int *,这就是为什么我们编写

*((int *)one),就像我们有一个指针p一样我们用@ p取消引用




你看到发生了什么吗?


希望这会有所帮助,

Stu

Well...

one is of type const void * : we can''t dereference that (What''s a "void"?!
No, really, don''t answer that anyone...)

But we happen to know that one really points to an int and we want to get at
that int. "Hmm", we say, "if only we had a pointer of type int * to that
int..." As it happens, we can obtain one by casting one to type int *, i.e.
by writing (int*)one. But this still hasn''t given us our pointed-to int. For
that, we have to dereference this new int *, which is why we write
*((int*)one), just as if we had a pointer p and we were dereferencing that
using *p.

Do you see what''s going on?

Hope this helps,
Stu


st*******@gmail.com 写道:

我在c ++学习新手...我有以下内容问题...

阅读一些源代码,我看到了这个:


int函数(const void * one,const void * two)

{

int var1,var2;

var1 = *((int *)one);

var2 = *(( int *)two);

/ * sm其他代码在这里* /

}


我的问题是关于rhs的var1 assigment。 - *((int *)one);

i确定它的作用; P

我的问题是如何完成的...我的意思是,为什么这么多的解引用

运营商和如此多的括号?难道不是那么复杂吗?


有人可以解释这是如何工作的吗?
i''m a learning newbie at c++... and i have the following question...
reading some source code, i saw this:

int function(const void * one, const void * two)
{
int var1, var2;
var1 = *((int*)one);
var2 = *((int*)two);
/* sm other code here*/
}

my question is about the rhs of var1 assigment. -*((int*)one);
i am sure about what it does ;P
my question is how this is done.. i mean, why so many dereference
operators and so many parentheses? isn''t that to complicated??

can someone explain how this works??



让我们一步一步看看:


一个是const void *,所以它是一个指向某个未知类型的指针,这个

未知类型是const,所以它不能在这个

函数的上下文中更改。


(int *)one是这个未知指针的强制转换。它假设一个是一个指向int的指针,并且这样做一个指针。


*((int *)one)取消引用这个指针。由于已经将一个指针转换为指向int的指针,因此取消引用它将产生一个int(假设

指针有效,并且它确实指向一个int )。


写这个函数的方式非常不安全。您无法保证指针实际指向int的
。该函数还可以确定
不检查空指针的可能性。


指针是一个高级主题。 C ++有很多技巧可以让你在许多情况下避免使用指针,但对于其他一些情况,他们可能需要
。但是,在有必要的情况下,它可以用某种智能指针代替它们,这可以帮助减轻一些复杂性。正确使用它们。


-

Marcus Kwok

用''net''替换''invalid''回复

Let''s look at it step-by-step:

one is a const void*, so it is a pointer to some unknown type, and this
unknown type is const so it cannot be changed in the context of this
function.

(int*)one is a cast of this unknown pointer. It assumes that one is a
pointer to an int, and casts one as such.

*((int*)one) dereferences this pointer. Since one has been casted to a
pointer to int, dereferencing this will yield an int (assuming that the
pointer is valid, and that it is indeed pointing to an int).

The way this function is written is very unsafe. You have no guarantee
that the pointer is actually pointing to an int. The function also does
not check for the possibility of a null pointer.

Pointers are an advanced topic. C++ has many techniques that will allow
you to avoid using pointers in many cases, but for some other cases they
may be necessary. However, in the cases where they are necessary, it
may be possible to replace them with a smart pointer of some kind, which
can help alleviate some of the complexity of using them properly.

--
Marcus Kwok
Replace ''invalid'' with ''net'' to reply


首先,感谢你们两位的答案:)
firstly, thank both of you for the answers :)

> Stuart Golodetz

当发生这种情况时,我们可以通过将一个类型转换为int *来获得一个,即通过写入(int *)一个。但是这仍然没有给我们指向我们的指针。为此,我们必须取消引用这个新的int *,这就是我们写> *((int *)one)的原因,就好像我们有一个指针p并且我们使用* p取消引用它。
<你看到发生了什么事吗?
>Stuart Golodetz

As it happens, we can obtain one by casting one to type int *, i.e. by writing (int*)one. But this still hasn''t >given us our pointed-to int. For that, we have to dereference this new int *, which is why we write >*((int*)one), just as if we had a pointer p and we were dereferencing that using *p.

Do you see what''s going on?



所以,斯图尔特,我想我...... ..

令人困惑但是......:

so, Stuart, i think i do.. :)
confusing though.. :


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

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