Starting to understand pointers, just need a few clarifications

So I have recently been investing a bit of time in understanding pointers since they are a joy to my life. I have the basic idea of them and understand how they work. I think I just need to use them and it'll eventually become second nature to me. There is something I do not understand though and is kind of hard to search on the internet.

I understand that *this is a pointer and all but when do you use the -> method?

I read somewhere that you use that for pointing to functions? In any case, could someone please clarify when and why you would use -> ?

And lastly, I'll post an example, one particularly with the project I am working with. Could you possibly explain what this line of code is saying, without me providing you with all the code. This line of code:

itemToOrder->amount += numToOrder ;

Is it saying the itemToOrder is pointing to the address of the ( amount = amount + numToOrder ) and is then itemToOrder equals the value at that address?

Thank you for all of your help!
The arrow operator (->) is just a combination of the * and . operators.

1
2
3
4
5
// this...
foo->bar;

// is the same as this:
(*foo).bar;


* dereferences a pointer and gets you an object
. takes that object and gets one of its members
-> does both... it dereferences a pointer and gets one of its members.
p->m translates directly to (*p).m. They mean exactly the same.

itemToOrder->amount += numToOrder ;
Translated:
(*itemToOrder).amount += numToOrder ;
Dereference pointer (i.e. get the thing to which it points): *itemToOrder
Access class member 'amount': (...).amount
Add numToOrder to the member and store the result in the member: ... += numToOrder
That helps out so much! So -> will only be used for class members, correct?
Yes. class/struct/union members only.
Thank you guys so much
Topic archived. No new replies allowed.