I was looking at some c++ code, and I saw this:
int num = 开发者_如何学C*(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?
The (int *) part casts the variable number to a pointer to an int, then the * in front dereferences it to an int.
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
.
So, (int*)number
is "the original pointer, converted to an int*
so that I can read an int
from it", and *(int*)number
is the int value that it points to.
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?
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.
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.
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.
I'm assuming customer
is used like this:
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.
Note using void *
for the argument is probably not the best, since you could do:
double lookup = 69.0f;
customer_key *key = customer(&lookup);
And this will compile, but won't look up customer 69 (a double
is not an 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.
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
int num = *(int *)number;
typically, 'number' here should be a pointer with some type, usually a void*
pointer.
(int *)number
, means you cast the original type to int*
, and *(int *)number
, means you get the value of int pointer.
精彩评论