J
johny smith
I have never really understood the purpose of the void*.
I guess it is so different data types could for instance be passed into a
function. And then the function will re cast it to the correct data type?
I have an example here that does not seem to work.
I pass in an integer in one case and a float in another case.
When I pass the float, I would expect to get an answer of 5 but I get a huge
number that is not 5.
So, I was wondering if someone could tell me what I am doing wrong here?
Thanks.
I assume this is a strange way of using the void*, but I was just wanting to
practice using it in some way.
#include <iostream.h>
void f1( void* number );
int main()
{
int number;
float number2;
number2 = 5.25;
number = 5;
f1( &number );
f1( &number2 );
return 0;
}
void f1( void* number )
{
int* num;
num = (int*)number;
cout << *num << endl;
}
I guess it is so different data types could for instance be passed into a
function. And then the function will re cast it to the correct data type?
I have an example here that does not seem to work.
I pass in an integer in one case and a float in another case.
When I pass the float, I would expect to get an answer of 5 but I get a huge
number that is not 5.
So, I was wondering if someone could tell me what I am doing wrong here?
Thanks.
I assume this is a strange way of using the void*, but I was just wanting to
practice using it in some way.
#include <iostream.h>
void f1( void* number );
int main()
{
int number;
float number2;
number2 = 5.25;
number = 5;
f1( &number );
f1( &number2 );
return 0;
}
void f1( void* number )
{
int* num;
num = (int*)number;
cout << *num << endl;
}