pointer example

hello when i was reading the documentation i found an example but i didn't quite understand it. down here ill send the example with comments on how i think i should look at them. if someone else sees a mistake please tell me. thanks in advance.

example:
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
void increase (void* data, int psize)
 // the void value pointed by data = the adress of a
{
  if ( psize == sizeof(char) )        
// in case of increase (&a,sizeof(a));
  { 
    char* pchar;
// declaration pointer pchar (points to a char)
    pchar=(char*)data;                
//pchar = the char value pointed by data = the adress of a
    ++(*pchar);                      
//pchar points to the next addres (x --> y)
  }
  else if (psize == sizeof(int) )     // in case of increase (&b,sizeof(b));
  { int* pint; pint=(int*)data; ++(*pint); } //same stuff here but with ints
}

int main () //i understand this
{
  char a = 'x';
  int b = 1602;
  increase (&a,sizeof(a));
  increase (&b,sizeof(b));
  cout << a << ", " << b << endl;
  return 0;
}
Changed some comments and removed the comments you understood.
1
2
3
4
5
6
7
8
9
10
11
12
13
void increase (void* data, int psize)
 // the void value pointed by data = the adress of a
{
  if ( psize == sizeof(char) )  
  { 
    char* pchar;
    pchar=(char*)data;  //cast data to a char* , assign that value to pchar;         

    ++(*pchar); //dereference the pointer (pointer still points to same address, the value at that address is incremented 'x' now = 'y'           
  }
  else if (psize == sizeof(int) )     
  { int* pint; pint=(int*)data; ++(*pint); } //same stuff here but with ints
}
Topic archived. No new replies allowed.