Pointers, when to use syntax

Can someone explain to me please when you can use -> or . operator to dereference a pointer?

For example, if you have NodeType *ptr = new NodeType

then ptr can only use -> syntax to dereference but not the . operator

why is that?

I looked at the stack overflow forums but I'm still a little confused
Because ptr is a pointer. If use declare like this:
NodeType p;
Then you can use . operator.
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

struct dummy
{
   int first;
   int second;
};

int main()
{
   dummy  aVar;  
   dummy* pVar = new dummy;
   
   aVar.first = 3;
   (&aVar)->second = 15;
   std::cout << (&aVar)->first << ", " << aVar.second << "\n";

   pVar->first = 9;
   (*pVar).second = 225;
   std::cout << (*pVar).first << ", " << pVar->second << "\n";
   delete pVar; 
}
I'll just say it simply.
as you know, *ptr is the value, and ptr is the address.
so if you had an object named bob from your structure
You would use bob like *ptr.
*ptr.name
and
bob.name.
Then when you use the address version of the pointer,
You use the ->.
ptr->name is equivalent to *ptr.name
To reiterate, ptr is the address, and *ptr is the value.
Topic archived. No new replies allowed.