void Pointers

Can someone comment out each line of code? Explain in simple English.

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;
You seem to be posting threads like this a lot.

How about this. You try to comment the code yourself as best as you can understand it. Then we can clear up any confusions and/or correct any mistakes.
Where did you get the code from, because if you do not understand it, then you should not be using it, let alone asking others to help you with it.
I tried my best in order to comment the code. I now appreciate if someone can correct my mistakes. There were a few lines that I really don't know what is happening. Why is increase and data have a void datatype and not an int for char for example? It gets confusing at times.


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

// increaser
#include <iostream> // input / output stream from keyboard
using namespace std; // using the standard namespace
 
void increase (void* data, int psize) { // creating an increase function with no return data hence void.
// we declare two local variables void* data where a pointer is ponting to the varaible data and we created another variable declaring it as int psize

  if ( psize == sizeof(char) ) // if condition checks whether psize is equal to the size of the char data type (which is 1 byte) 
        { char* pchar; pchar=(char*)data; ++(*pchar); } THIS IS THE LINE WHICH I DON'T REALLY UNDERSTAND!
// if condition evaluates to true, the char variable points to pchar variable where we get the value stored of the varaible, pchar variable is equal to what is stored in char, add to what is stored in the pchar variable

  else if (psize == sizeof(int) ) // if the first if isn't evaluated to true, it checks the condition and see whether psize is equal to the size of int in bytes (which I assume is 4 bytes)
        { int* pint; pint=(int*)data; ++(*pint); } THIS IS THE LINE WHICH I DON'T REALLY UNDERSTAND! 
// if condition evaluates to true, the int varaile points to pint varable where we get the value stored of the varaible, pint variable is equal to what is stored in data, add to what is stored in the pint variable
}
 
int main () { // program execution starts from here
 
char a = 'x'; // we declare a character a and assign x to it in memory
int b = 1602; // variable b is stored as an integer in memory and is assigned the value 1602

increase (&a,sizeof(a)); // HERE, I DON'T UNDERSTAND THIS LINE EITHER.
increase (&b,sizeof(b)); // HERE, I DON'T UNDERSTAND THIS LINE EITHER.

        cout << a << ", " << b << endl; // output the values stored in variables a and b to screen
return 0; // exit code
Last edited on
Why did you double post? I literally answered your question the first time:
http://www.cplusplus.com/forum/beginner/104855/
Yes, but I didn't actually understand yours. Sorry for double posting. I didn't do it on purpose.
Didn't mean to sound snappy.

To clarify, let's expand the line of code you don't really understand like so:

1
2
3
4
5
if(psize==sizeof(char)) {
	char* pchar;
	pchar=(char*)data;
	++(*pchar);
}


The first line is an if statement which compares the psize variable (which is passed as an argument to the function) and the size in bytes of the char integral type, which we can safely assume to be 1 byte.

If the condition is met (that psize is equivalent to the size in bytes of a char), execution continues in the branch.

The second line instantiates a character pointer with the name pchar.

The third line initializes pchar to point to data which was passed as an arguement.

The fourth line increments the value of what pchar is pointing to by 1.

Will edit this if more information is requested.
*EDIT* typo.
Last edited on
Topic archived. No new replies allowed.