C++ *Pointer vs. Pointer* and Member Access Operator

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Member Operator
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
ineedhelpnow
Messages
649
Reaction score
0
A pointer in C++ is represented by *. Sometimes the * comes after the variable/class/whatever such as 'Pointer*'. Other times it comes before, '*Pointer'. What is the difference between the two?What is the member access operator for? (->) According to my notes, a->b is equivalent to (*a).b
 
Physics news on Phys.org
The C++ syntax is context-sensitive, so the meaning of * depends on the surrounding code. In declarations, it generally comes after the type, i.e. char* x declares a pointer to a char. In an expression, *x dereferences the variable x (which needs to be a pointer) and so *x is the original object pointed at by x (which may itself be a pointer, if x was a pointer to a pointer, for instance). You can then access its members, if it has any, via (*x).blah.

The operator -> is indeed just a handy shortcut for referencing a pointer and accessing one of its members. No doubt the language designers thought it would be convenient because of less brackets, and also more readable. And also it is less prone to precedence errors, i.e. typing *a.b instead of (*a).b, whereas there is no such ambiguity with a->b.

So -> is not strictly necessary, but it is useful nonetheless. You could also argue * is not necessary since you can dereference a pointer by treating it as an array, i.e. *x is the same as x[0]. Or the other way around, by accessing array elements through pointer offsets. Basically, there are many ways to achieve the same thing in C/C++. Did you know 5[x] is valid syntax and absolutely equivalent to x[5] if x is a pointer/array?
 
Bacterius said:
Did you know 5[x] is valid syntax and absolutely equivalent to x[5] if x is a pointer/array?

No, I never knew that.