We help IT Professionals succeed at work.
Get Started

why "." vs "->"

rickhill11
rickhill11 asked
on
242 Views
Last Modified: 2016-01-09
Im an old, emphasis on old, K&R guy, so I am very well versed in accessing members of structures, unions, and now classes with "." notation vs "->" notation.  I was asked this morning "why the difference?"  My answer started with "That's easy," and then every instance that I could come up with I ended up deciding that a modern compiler could easily overcome the issues.  

So, given a structure or class foo with a single member "a"

I understand that
       struct foo *pfoo, afoo;
       pfoo=&afoo;
       pfoo->a would be the appropriate call

OR
      struct foo afoo;
      afoo.a would be the appropriate call

but why are they separate.  In case 1, why can't the compiler sort out pfoo.a or in the second afoo->a.  There must be a case where this behavior would be unacceptable, but I am trying to fathom what it is.
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This problem has been solved!
Unlock 5 Answers and 23 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE