Feb 10, 2012 at 5:45pm UTC
i was looking at an example of pointers and it did a char with value x and the output is y after doing ++ to it.
here is 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;
}
output:
Last edited on Feb 10, 2012 at 5:46pm UTC
Feb 10, 2012 at 5:48pm UTC
The ASCII code for 'x' is 120 (0x78). Adding one brings up 'y' 121 (0x79).
Feb 10, 2012 at 6:04pm UTC
ah ok but ive read in the chapter of pointers that we cant know the location of the address i point to yet
Feb 10, 2012 at 6:09pm UTC
& - ampersand, Reference operator (&).
http://www.cplusplus.com/doc/tutorial/pointers/
Quote from the link:
The address that locates a variable within memory is what we call a reference to that variable. This reference to a variable can be obtained by preceding the identifier of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of".
Last edited on Feb 10, 2012 at 6:10pm UTC