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
Click For Summary
SUMMARY

The discussion clarifies the usage of pointers in C++, specifically the syntax variations of the asterisk (*) and the member access operator (->). The asterisk can appear before or after a variable, with its meaning determined by context; for instance, 'char* x' declares a pointer, while '*x' dereferences it. The operator '->' serves as a shorthand for accessing members of an object pointed to by a pointer, enhancing readability and reducing potential precedence errors. The conversation also highlights alternative pointer usage, such as treating pointers as arrays, exemplified by the equivalence of '5[x]' and 'x[5]'.

PREREQUISITES
  • Understanding of C++ pointer syntax
  • Familiarity with member access operators in C++
  • Knowledge of context-sensitive syntax in programming languages
  • Basic concepts of arrays and pointers in C++
NEXT STEPS
  • Explore C++ pointer arithmetic and its applications
  • Learn about C++ smart pointers and memory management
  • Investigate the differences between C-style arrays and C++ vectors
  • Study the implications of pointer aliasing in C++
USEFUL FOR

C++ developers, software engineers, and computer science students looking to deepen their understanding of pointer mechanics and member access in C++ programming.

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
 
Technology 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.
 

Similar threads

  • · Replies 40 ·
2
Replies
40
Views
4K
Replies
12
Views
2K
  • · Replies 19 ·
Replies
19
Views
6K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
86
Views
2K
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
11K