Things you can do with pointers that you can't do (or are very difficult) otherwise:
Use memory that isn't on the stack (your stack is probably limited to a few megabytes - want more than that? Pointers ahoy).
Directly interface with hardware (hardware uses fixed memory locations - I'd like to see you access one without a pointer).
Polymorphism.
I would think that *x=&y is assigning the address of y to the address pointed to by x, where ever x points, where x=y is assigning the value contained in y to address pointed to by x.
well no its not exactly the same, but either way x gets the value of y.
maybe I was thinking of &y = *x.
Can you give me an example of another way to use them. Most of what I know in C++, I dont really know by its proper term so explaining it usually confuses me more.
int hugeArray[100000000]; // Oh no, causes a stack overflow! I can't make an array this big
int* pToHugeArray = newint[100000000]; // Yay! Now I CAN have an array that big, but I have to use a pointer
// This works because the first array was being made on the "stack", which is not very big, but new
// gets space from the "heap" which is way bigger :)
Let's check the manual for this piece of hardware. Aha, the input voltage from the sensor can be read at memory location 0x1234FFDD, where it is presented as a four byte integer. I want to read that value. Luckily, I have pointers, otherwise I would have no way to explicitly identify a known memory location. I happen to know that my system uses four byte integers, so I can interpret the value directly as an int.
1 2 3
int* pointerToVoltageReading = (int*)0x1234FFDD;
// Now, what's that voltage reading?
int voltage = *pointerToVoltageReading;
I want to make an array, but I don't know how big it needs to be. That will be decided at runtime, perhaps by the user entering a value.
I'll try this:
1 2
cin >> n; // Fetch value from user
int someArray[n]; // Make the array. OH NO! C++ forbids arrays of variable size like this!
What shall I do? Hmm... I could use a C++ container like a vector, but I'd like to keep this as an array. Aha, I know! I can use the new command to make an array of variable size at runtime, like this:
1 2
cin >> n; // Fetch value from user
int* pointerToArray = newint[n]; // new returns a pointer, so I'll have to use that pointer to access the array
Moschops also mentioned polymorphism. This is very powerful.
I'm working on a card game just now (rummy). There are 2 hands, a deck, a discardPile, and 2 vectors of playStacks (where the players place their card plays on the table). All of these derive from a cardStack base class (since they all have different behaviors). Cards move from the deck to the hands, to/from the discardPile and the playStacks but in all cases the code for this is simply p_src->sendCard( p_dest );. Here p_src and p_dest are pointers to 2 cardStacks of whatever type. Cool stuff!
* is the dereference operator, it returns the value at the address that the pointer points to. You use it to declare a pointer too. -> is the member selection operator used with pointers. Instead of writing (*pntr).classMember, which looks ugly, you use -> and write pntr->classMember, which looks more understandable.
Would pntr and cSomething ever have different values?
They always have different values. One of them is a pointer. Its value is a single number. One of them is an object of type Something. Its "value" doesn't make a lot of sense, but it has internal members that themselves can have values.
The value of the pointer in the case above is equal to the address that the Something object is sitting in. That doesn't mean they have the same value. I'm not sure you've understood what a pointer is. Read this: