Nov 30, 2011 at 7:52pm UTC
I m a beginner need a help in void pointer.
In the program given below can u explain what does pint=(int*)data; means in function increase.
#include <iostream>
using namespace std;
void increase (void* data, int psize)
{
if ( psize == sizeof(char) )
{ char* pchar; pchar=(char*)data; ++(*pchar); }
else if (psize == sizeof(int) )
{ int* pint; pint=(int*)data; ++(*pint); }
}
int main ()
{
char a = 'x';
int b = 1602;
increase (&a,sizeof(a));
increase (&b,sizeof(b));
cout << a << ", " << b << endl;
return 0;
}
Thanks for help :)
Nov 30, 2011 at 8:02pm UTC
A void pointer has no specific type and can be cast to any type of pointer.
In your case, the void pointer data
is cast to an int pointer, to fit the type of pint
, and gets loaded into pint
.
Dec 2, 2011 at 7:27am UTC
Thanks for help wjee0910.