| BrokenSilenceDev (65) | |
What exactly is the point of pointers. From what I've seen all you can with them is:*x=&ywhich is the same as x=yWhat else can they do? | |
|
Last edited on
|
|
| Moschops (3712) | |
|
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
|
|
| ne555 (2984) | |
| Point. | |
|
|
|
| ResidentBiscuit (970) | ||
| ||
|
|
||
| hanst99 (2697) | |
| Building dynamic data structures, passing larger data structures around without having to make copies of them on the stack... | |
|
|
|
| Grey Wolf (2909) | |||
| |||
|
|
|||
| roberts (313) | |
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.
| |
|
|
|
| Moschops (3712) | ||
You're right, it's not. | ||
|
|
||
| BrokenSilenceDev (65) | |
|
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. | |
|
|
|
| Moschops (3712) | |||
| |||
|
Last edited on
|
|||
| Moschops (3712) | |||
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.
| |||
|
Last edited on
|
|||
| Moschops (3712) | |||||
|
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:
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:
| |||||
|
Last edited on
|
|||||
| fun2code (821) | |
|
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!
| |
|
|
|
| BrokenSilenceDev (65) | |
|
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. | |
|
|
|
| Wisely Done (74) | |
* 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.
| |
|
|
|
| BrokenSilenceDev (65) | |
| So it looks for a member at a class address, or the member value address? | |
|
|
|
| Wisely Done (74) | |||
Example:
| |||
|
|
|||
| BrokenSilenceDev (65) | |
|
ok thanks. Would pntr and cSomething ever have different values? | |
|
|
|
| Moschops (3712) | |
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
|
|
| Wisely Done (74) | |||
|
Another example:
| |||
|
Last edited on
|
|||