void* as argument in function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

void* as argument in function

Had someone suggest using void *context in a function, statement was the function doesn't need to know anything about the variable being passed. In the particular call. I changed some code from: fun(type name); to fun(void *context); How does this optimize or make the program better? https://code.sololearn.com/cCctvUp8qtMf

13th Mar 2022, 9:56 PM
William Owens
William Owens - avatar
1 Answer
+ 6
Not sure about optimization or whether `void*` makes a program better. Just as I understand it, we pass a `void*` to a function to sort of "imitate" the polymorphism principal, in that we can use various types of data inside the function, we just need to dereference and cast the `void*` properly to a variable (of the actual type) we want to work with. Actually, I'm not sure what benefit `void*` brings in this case, cause it seems that `float` be the only type that was used by the functions which uses `void*` parameters ...
14th Mar 2022, 1:19 AM
Ipang