Sum with int and char returns weird char

Hello again.
I am trying to sum a char with an integer and get a new char (the corresponding ASCII value between 32 and 128 of the sum)

So I have:
1
2
3
int val1 = 1;
char* val2 = new char[length]; // I get this char array from an EDIT control !!!
char result = val1 + val2;


So, if val2 would be 'a', the sum would be:
ASCII(a) = 97 (integer);
97 + 1 = 98(integer)
The ASCII value for 98 is 'b', which should be the result BUT

When I click on the button that executes the function I get a weird result of the sum of these two variables, something like b&V, or b&Ú (it changes with each new start of the program). If I click several times it returns the correct result ('b'). The result is shown inside another EDIT control. I don't know if it is because the EDIT control sends hidden chars to val2 or something happens when the integer result is converted to a char.
Thanks for your help!
Is that code even valid? val1 is a pointer and adding one you a pointer to val2[1]. Then you try to assign a pointer to a char, which I'm not so sure you can do.


But you are right that adding 1 to 'a' should give 'b'.
1
2
char a = 'a';
char b = a + 1;
Topic archived. No new replies allowed.