I started learning Cpp after Python, so some results are not really obvious for me.
I have this code:
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int *b = &a;
b += 1;
cout<<b;
return 0;
}
And it returns different value every time, for example 0x7fff0dabe070.
I know this 0x means hexadecimal number, but why doesn’t it just return 11 or at least the same number every time?
>Solution :
To get the expected result you should write
int a = 10;
int *b = &a;
*b += 1;
cout<<*b;
Othewise in this statement
b += 1;
there is used the pointer ardithmetic and in this statement
cout<<b;
there is outputed the address of the memory just after the object a stored in the pointer after the pointer arithmetic
To get a value of an object pointed to by a pointer you need to dereference the pointer.