Reading pointers finally make sense and I just want to share and be told off if I'm wrong. It's all down to the names and the equals sign!
So,& gives the reference,These are inverse operators so applying both is like doing nothing. A lot like multiply by 2 and divide by 2 or add two and subtract 2. More formally, a/a = 1 and 1 + a - a = 1. So following on *& = 1.
* is the dereference.
Consider the snippet from Cprogramming.com pointers,
Now the bit that clicked for me was this,Code:int x; // A normal integerint *p; // An pointer to an integerp = &x; // Assign the address of value x to p cin >> x; // Give x a value cin.ignore(); // Remove the carriage return from the entry cout << *p << "\n"; // This will print the value entered for x
so if I apply the * operator, I must apply to both sidesCode:p = &x
but the operators are inverses on the right hand sideCode:*p = *&x
the last line would act likeCode:*p = xand it does.Code:cout << x << "\n";
Hopefully someone more practiced can say if this is a good schematic view. I've not seen this mentioned elsewhere and hope it helps someone who is stuck with pointers but thinks like a mathematician, got to be one or two out there.



LinkBack URL
About LinkBacks



