Square function casting in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Square function casting in C

"This square function has been written to multiply ints, which is why the num void pointer is cast to an int. If the implementation were to be changed to allow square() to multiply floats, then only the definition need be changed without having to make changes to the declaration." Then Can you suggest me a code with editing following code only by changing definition. I am completely new to C. https://code.sololearn.com/cbAAS26GeVK6/?ref=app

15th Apr 2023, 2:12 AM
Amal Benny
Amal Benny - avatar
2 Answers
+ 4
void* square (const void *num) { static float result; result = (*(float *)num) * (*(float *)num); return(&result); } Of course, the stuff in the main function needs to be adjusted to using float. But the point that this lesson wants to bring home is that the function declaration, void* square (const void *); has not changed.
15th Apr 2023, 5:11 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 3
Right :) But I also said that you need to adjust the stuff in main ;) Think about what you are returning now, and how you need to change the main function to make it all fit together.
25th Apr 2023, 1:33 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar