void pointer - do not understand code

I am working through the C++ guide from cplusplus.com. I do not really understand the following code (esepcially the emboldened code). What exactly is the emboldened section doing?

please could someone walk me through what the int main () is doing with the function and what the function does.

This is what I understand:
1) variable a is defined as a char and assigned value x
2) vaiable b is defined as int and assigned value 1602
3) increase (&a, sizeof(a)) assigns &a to data and 1 to psize (as char = 1)
4) the function defines a pointer variable pchar
5) pchar = (char*)data - this line is the most confusing. Firstly what is it doing. second, why could it not read char*data?
6)++(*pchar) increases the location that pchar points to by 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 #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; 
}
void pointer is abstract poiinter to some data. It does not has any information about typem size or how to handle data.
To manipulate it you should cast void pointer to pointer to some other type. This function selects one of the two types depending on size and does increments it. That allows you to have one function receiving different data. That was method of making generic functions in C: you pass void pointer, type size and it does some manipulations with it. That how qsort C function works: http://en.cppreference.com/w/c/algorithm/qsort
pchar= (char*)data

why can this NOT be written

pchar = char*data

what is the significance of the brackets?
pchar= (char*)data ← converting data variable to the type char* (pointer to char)
pchar = char*data ← completely invalid. It would mean "multiply char and data variables" if it wasnt forbidden to use reserved words as variable names
pchar= (char*)data; is casting (i.e. converting type) by using the C's cast syntax. C++ has a more finetuned syntax. See http://www.cplusplus.com/doc/tutorial/typecasting/
thanks all :)
Topic archived. No new replies allowed.