Addressing and Dereferencing

Started by beingchinmay, 09-03-2016, 03:44:48

Previous topic - Next topic

beingchinmayTopic starter

As in C, the dereferencing, or indirection, operator * is unary and has the same precedence and right-to-left associativity as the other unary operators. If p is a pointer, *p is the value of the variable that p points to. The direct value of p is a memory location, whereas *p is the indirect value of p—namely, the value at the memory location stored in p. In a certain sense, * is the inverse operator to &. Here is code showing some of
these relationships:


Quoteint i = 5, j;
int* p = &i; // pointer p is init to address of i
cout << *p << " = i stored at " << p << endl;
j = p; // illegal pointer not convert to int
j = *p + 1; // legal
p = &j; // p points to j