# include <stdio.h> int main() { float i = 10, *j; void *k; k = &i; j = k; printf("%f", *j); return 0; } The above gives output 10.000000 in the GCC compiler.
My doubt is, we should write the expression j = k as j = (float *)k, right?
No, it is not required. As per C11, chapter §6.3.2.3,
A pointer to
voidmay be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer tovoidand back again; the result shall compare equal to the original pointer.
So, in your case,
k = &i; // no cast needed j = k; // again, no cast needed is functionally same as
j = &i; No explicit cast is needed here.
void *. That's why using functions likemallocwithout casting works.