Uses for Pointers

Pages: 123
What exactly is the point of pointers. From what I've seen all you can with them is:
`*x=&y`
which is the same as
`x=y`
What else can they do?
Last edited on
Oh God. Where's that FAQ got to?

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.

Last edited on
Point.
 Point.
Building dynamic data structures, passing larger data structures around without having to make copies of them on the stack...
Moschops wrote:
Oh God. Where's that FAQ got to?
Feel free to draft an entry for the FAQ. All help is appreciated . :0)
Is `*x=&y` the same as `x=y`?

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.
 Is *x=&y the same as x=y?

You're right, it's not.
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.
 ``12345`` ``````int hugeArray[100000000]; // Oh no, causes a stack overflow! I can't make an array this big int* pToHugeArray = new int[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 :) ``````
Last edited on
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.

 ``123`` ``````int* pointerToVoltageReading = (int*)0x1234FFDD; // Now, what's that voltage reading? int voltage = *pointerToVoltageReading;``````
Last edited on
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:

 ``12`` ``````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:

 ``12`` ``````cin >> n; // Fetch value from user int* pointerToArray = new int[n]; // new returns a pointer, so I'll have to use that pointer to access the array ``````
Last edited on
closed account (D80DSL3A)
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!
ok thanks
I have one other problem though.

What exactly do * and -> point to?
I read the entry on the tutorial but I doesnt rally make a lot of sense to me.
`*` 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.
So it looks for a member at a class address, or the member value address?
Example:

 ``1234567891011121314151617`` ``````class Something { public: void print() { std::cout << "Hello World!" << std::endl; } }; int main() { Something cSomething; Something *pntr = &cSomething; cSomething.print(); pntr->print(); }``````

ok thanks.
Would pntr and cSomething ever have different values?
`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:

http://www.cplusplus.com/articles/EN3hAqkS/

and other such in the Articles section.
Last edited on

Another example:

 ``1234567891011121314151617181920212223242526`` ``````#include class Something { public: int value1; }; int main() { Something cSomething; Something *pntr = &cSomething; cSomething.value1 = 5; std::cout << cSomething.value1 << std::endl; //This will print 5 std::cout << pntr->value1 << std::endl; // This will print 5 pntr->value1 = 6; std::cout << cSomething.value1 << std::endl; // This will print 6 std::cout << pntr->value1 << std::endl; //This will print 6 } ``````

Last edited on
Pages: 123