I'm having a bit of trouble in understanding this piece of code.
double F(double k,void *params){
double *p=(double *)params;
return p[0];
}
I have only a small notion of pointers from a class, and I understand that in the function his using the pointer *params as an argument. I also understand that he uses double *p to declare the variable. What I can't figure out is what (double *)params do, or how p got turned into a vector.
CodePudding user response:
The expression (double *)params
means "treat params
as a pointer to double
instead of a pointer to void
". The (double *)
is a cast.
Under most circumstances you cannot copy a pointer value of one type to a pointer variable of a different type (you can't assign a double *
value to an int *
variable) without an explicit cast.
The one exception in C is assigning a void *
value to a different pointer type or vice versa - in this particular case, the cast is unnecessary. However, C does not allow implicit conversion from void *
to other pointer types, so in that language the cast is necessary.
Pointers to different types do not have to have the same size and representation; the only rules are
char *
andvoid *
have the same size and alignment;- Pointers to qualified types have the same size and alignment as pointers to the unqualified equivalent (e.g.,
const int *
andvolatile int *
have the same size and alignment asint *
); - Pointers to all
struct
types have the same size and alignment; - Pointers to all
union
types have the same size and alignment;
CodePudding user response:
(double *)params
instructs the compiler to think of params
(which is a void pointer originally - the compiler has no idea what type it's pointing to) as a pointer to a double only while processing this expression. It's very commonly known as a "C-style cast of one type to another". In this case, we are "casting" params
from a void*
type to a double*
type, since we are storing it in the pointer to double double *p
.
Without the cast, the compiler will most likely give you a type mismatch
error/warning, since you would be trying to store the value of a void pointer into a pointer to double.
More specifically, when you have a double pointer, or pointer to anything except void, the compiler associates a number with it in order to be able to perform pointer arithmetic on it when you write something like (ptr )
. For example, when doing ( ptr)
on a pointer to double, the compiler knows how many bytes to go up by, because it knows the size of a double. However, a void* has no size associated with it, therefore the compiler can't perform pointer arithmetic on it. So when casting the void pointer to a double pointer using (double*)params
, you're allowing the compiler to associate the standard size of variables of type double
with whatever this void pointer is pointing to only while processing this expression, for the purposes of storing its value inside an actual pointer to double. After the compiler is done reading this expression, it goes back to thinking params
is a void pointer, not a double pointer, ie the effect of casting it to a double pointer is not permanent, it's only valid during the processing of this expression by the compiler.