x+1=y?

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:
y, 1603
Last edited on
The ASCII code for 'x' is 120 (0x78). Adding one brings up 'y' 121 (0x79).
ah ok but ive read in the chapter of pointers that we cant know the location of the address i point to yet
& - 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
ah ok but ive read in the chapter of pointers that we cant know the location of the address i point to yet


This isn't incrementing the pointer, it is incrementing the value in memory the pointer is pointing to.

++(*pchar);
Broken down in order.

(*pchar) //dereferences the pointer. Any operator acting on this is acting on 'x' or 120

++('x') //translates to ++(120), same as a loop using counter ++i

ok thank you.
Topic archived. No new replies allowed.