Tuesday, 12 December 2017

c - Necessity of use (float*) before malloc

malloc will give you a pointer to
void that you can't use for anything related to things you want
to do with a float. To be able to use the variable allocated at
the returned memory location, you need to cast it to a float*
so you can dereference that pointer and use it as a
float.




But,
as you've written your question, you should cast the return value of
malloc to float* and then immediately
dereference it before assigning it to x, since you've not
declared x as a pointer to
float.



EDIT:
As commenters pointed out, the explicit cast is only needed
in C++, not in C.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...