#include <iostream>
usingnamespace std;
void increase (void* data, int psize)
{
if ( psize == sizeof(char) )
{ char* pchar; pchar=(char*)data; ++(*pchar); }
elseif (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;
}
Basically it adds 1 to the first parameter of the "increase" function if it's an int, and it shows the next character of what seems to be a "list" with all characters (e.g if a = 'x', the result will be 'y'), if it's a char. My questions are: how is it possible to find the next charater? Are all the ASCII characters stored on a constant memory, from where they can be accessed?
A little explanation of how the code works would be great. Thank you.
Characters (or rather, values of type char) are integral numbers. With ASCII, 'x' equals 120 and when you increment it, you get 121 (='y').
See http://www.asciitable.com/
ASCII characters are represented directly with a numerical value.
http://www.asciitable.com/
The variable "a" is of type "char". It's initialized to the letter 'x' which actually is represented with a numerical value of 120 or 01111000 in binary (assuming that char is a single byte).
Every piece of data (struct, class, variable) is represented in binary form. The only way we know that it's actually an ASCII character is because of the typing system in C and C++.