0
# 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?

1
  • 1
    In C all pointers can implicitly be converted to and from void *. That's why using functions like malloc without casting works. Commented Apr 17, 2020 at 4:35

1 Answer 1

4

No, it is not required. As per C11, chapter §6.3.2.3,

A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and 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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.