C++ int num = *(int *)number; 这有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4170249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:42:16  来源:igfitidea点击:

int num = *(int *)number; What does this do?

c++cpointers

提问by Charles Ma

I was looking at some c++ code, and I saw this:

我在看一些 C++ 代码,我看到了这个:

int num = *(int *)number;

I had never seen this before? it was in a function labeled as such:

我以前从未见过这个?它在一个标记为这样的函数中:

void *customer(void *number){ }

What does that even do? Is there a different way to show this?

那到底有什么作用呢?有没有不同的方式来展示这一点?

Thanks, this isn't homework btw I was just confused at what this does?

谢谢,这不是作业顺便说一句,我只是对它的作用感到困惑?

回答by Charles Ma

The (int *) part casts the variable number to a pointer to an int, then the * in front dereferences it to an int.

(int *) 部分将变量 number 转换为指向 int 的指针,然后前面的 * 将其取消引用为 int。

回答by Steve Jessop

The function takes a void*, but somehow it knows (perhaps it's required in some documentation somewhere) that the pointer it's given actually points to an int.

该函数采用 a void*,但不知何故它知道(可能在某处的某些文档中需要它)它给出的指针实际上指向int.

So, (int*)numberis "the original pointer, converted to an int*so that I can read an intfrom it", and *(int*)numberis the int value that it points to.

所以,(int*)number是“原始指针,转换为 anint*以便我可以从中读取 an int”,*(int*)number是它指向的 int 值。

回答by Bill K

The correct answers are already here, but can I tell you a trick that generally helped me when I had to use C a lot?

正确答案已经在这里,但我能告诉你一个在我不得不大量使用 C 时通常对我有帮助的技巧吗?

It's how you pronounce "*" in your head--and there are two parts.

这就是你在头脑中发音“*”的方式——有两个部分。

The common part is when it is part of a type--and everybody probably says "pointer" when they read that, which is great. So (int *) is an int pointer--or I'll even reverse it in my head to read "pointer to an int" which seems to help a little.

常见的部分是当它是类型的一部分时——每个人在阅读时可能都会说“指针”,这很好。所以 (int *) 是一个 int 指针——或者我什至会在我的脑海中反转它来阅读“指向 int 的指针”,这似乎有点帮助。

The thing that helps a lot for me is whenever you see * in your code--read it as "what is pointed to by".

对我有很大帮助的事情是每当您在代码中看到 * 时——将其读作“指向的内容”。

If you follow this pattern, then:

如果你遵循这个模式,那么:

int num = *(int *)number;

is an integer variable "num" gets assigned the value: what is pointed to by an int pointer, number. It just translates itself.

是一个整数变量“num”被分配了值:int 指针所指向的值,number。它只是自己翻译。

Sometimes you have to mess with the phrasing a little, but since I got into that habit I've never had a big problem reading pointer code.

有时你不得不稍微弄乱措辞,但自从我养成这个习惯后,我在阅读指针代码时从来没有遇到过大问题。

I believe I also read & as "The address of" in C, but I think it's been overloaded in C++ if I recall correctly.

我相信我在 C 中也将 & 读为“的地址”,但如果我没记错的话,我认为它在 C++ 中被重载了。

回答by DVK

The function accepts a void pointer (thus void *). In order to dereference it to a variable of a certain type (e.g. int) - which is what teh first "*" does - you need to cast it to a pointer to an actual type - in this case to an int pointer via (int *) cast

该函数接受一个 void 指针(因此 void *)。为了将它解引用到某种类型的变量(例如 int)——这是第一个“*”所做的——你需要将它转换为指向实际类型的指针——在这种情况下,通过 (int *) 投掷

回答by ta.speot.is

I'm assuming customeris used like this:

我假设customer是这样使用的:

int lookup = 123;
customer_key *key = customer(&lookup);
// do something with key here

In which case, the code in customer is typecasting the void *to an int *and then dereferencing it (getting its value). It has to typecast first because void *basically means "pointer to something", which allows you to pass in any type you want. Without the typecast the compiler doesn't know if you want to read a char(usually 1 byte), a short(usually 2 bytes) or an int(usually 4 bytes). The typecast removes the ambiguity.

在这种情况下, customer 中的代码正在将 the 类型转换void *为 anint *然后取消引用它(获取其值)。它必须首先进行类型转换,因为void *基本上意味着“指向某物的指针”,它允许您传入任何您想要的类型。如果没有类型转换,编译器不知道您是否要读取 a char(通常为 1 个字节)、a short(通常为 2 个字节)或 an int(通常为 4 个字节)。类型转换消除了歧义。

Note using void *for the argument is probably not the best, since you could do:

注意void *用于参数可能不是最好的,因为你可以这样做:

double lookup = 69.0f;
customer_key *key = customer(&lookup);

And this will compile, but won't look up customer 69 (a doubleis not an int!).

这将编译,但不会查找客户 69(adouble不是int!)。

The use of void *may be intentional, the code may be able to determine (hopefully safely) between pointers and an argument like: (void *)3- which would be a special case.

的使用void *可能是有意的,代码可能能够(希望安全地)确定指针和参数之间,例如:(void *)3- 这将是一种特殊情况。