How does char++ function

Nov 10, 2013 at 6:18pm
I'm slowly working my way through the tutorials here but I came across something that I don't fully understand.

If anybody could explain to me how a character data type gets incremented or decremented I would be grateful.

I guess I'm just not sure how the data is stored and operated on in C++. Is it binary or hexadecimal or something else?

This is the example I was running through on the tutorial.

The one part of it I'm not fully understanding is the ++(*pchar). I understand that it's increasing the value of the data pointed to by pchar from x to y. I'm just not sure how the arithmetic actually gets processed.

Not sure if i've explained it well but hopefully you understand what i'm saying lol.

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 Nov 10, 2013 at 6:19pm
Nov 10, 2013 at 6:23pm
internally, a char is simply a binary number in the range -128 to +127. When it is displayed on the screen, we usually don't want to see the actual number, but rather the corresponding character symbol.
http://www.asciitable.com/
Nov 10, 2013 at 6:48pm
thank you for the fast reply. It's probably not required to know how the compiler actually does what it does.

I just like to try and understand how everything works.
Nov 10, 2013 at 6:59pm
I think its usually a good thing to have at least some understanding of how things work internally. It's the nature of C++ that some things don't fully make sense unless you know roughly what is going on. Though at the same time its hard to remember everything, and some things you just look up when required..
Topic archived. No new replies allowed.