Pointer 101: 1) A pointer holds a memory address. 2) You need to dereference a pointer to store something in it. 3) You use the dereference operator * to do that. 4) The * has a different meaning when you declare the pointer variable (even in the parameter list of a function).
Maybe a code pattern question: when is it preferable to use pointers to objects withing a class/struct (as the example above) rather than using a pointer to the class/struct itself to modify the members in the class/struct (as the example below)?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class MyClass
{
public:
int val;
MyClass(){val = 5; }
};
void function1(MyClass *c)
{
c->val = 10;
}
int main()
{
MyClass *c = new MyClass;
c->val = 5;
function1(c);
}