Just starting off, trying to understand pointers. The basic idea is ok. I am wondering why you can't increment a pointer in a loop and hence assign the referenced variable a new value this way.
Such as,
Code:
#include <iostream>
using namespace std;
int main()
{
int a; //int assigns 2 bytes of memory
int *b; //declare pointer b
b = &a; //set b to address of a
//*b= 0; //set dereference pointer(thus value of a = dereference value)
for(int i =0; i<10; i++)
{
cout <<a<<"\n";
*b++; //shouldn't this assigned a new value to a??
}
return 0;
}
If you say
Code:
int a=5;
int *b=&a
*b = 7
then a =7.
Thus a pointer allows you to both know the memory address of a variable(object) and also change that referenced variable through dereference.
So why doesn't the above loop change the value of a to increase by 1 each time?