void Pointers

I appreciate if someone can comment out every line of the below code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  
// increaser
#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;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// increaser
#include <iostream> //Include iostream
using namespace std; //using namespace std

void increase (void* data, int psize) { //function which takes a void pointer and an integer which will hold the size of data's type in bytes.
  if ( psize == sizeof(char) ) //if the size matches that of a char integral type...
	{ char* pchar; pchar=(char*)data; ++(*pchar); } //create char pointer, point to data, increment data.
  else if (psize == sizeof(int) ) // otherwise, if it matches the size of an integer integral type...
	{ int* pint; pint=(int*)data; ++(*pint); } //create int pointer, point to data, increment data.
}

int main () {

char a = 'x';
int b = 1602;
increase (&a,sizeof(a)); //pass a character to increase()
increase (&b,sizeof(b)); //pass an int to increase()
	cout << a << ", " << b << endl;
return 0;


I've loosely commented it for you, however, the code is pretty sloppy and bug prone. For instance, what if I pass a boolean? Or any other type for that matter? The byte size could potentially match between a type that's expected and one that isn't.
Topic archived. No new replies allowed.