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

In summary, in C++, the * symbol can have different meanings depending on the context. In declarations, it comes after the type to declare a pointer. In expressions, it dereferences a pointer and allows access to its members. The operator -> is a shortcut for accessing a member of a pointer. While not strictly necessary, it is convenient and less prone to errors. Additionally, there are other ways to dereference a pointer, such as treating it as an array or using pointer offsets. And interestingly, the syntax 5[x] is valid and equivalent to x[5] in certain cases.
  • #1
ineedhelpnow
651
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
  • #2
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?
 
  • #3
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.
 

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

What is the difference between C++ *Pointer and Pointer?

The main difference between C++ *Pointer and Pointer is the type of variable they represent. *Pointer is a pointer variable, which stores the memory address of a variable or object. Pointer, on the other hand, is simply a variable that holds a value, and can be of any data type.

When should I use *Pointer and when should I use Pointer?

You should use *Pointer when you need to manipulate or access the value stored at a specific memory address. This is commonly used when working with dynamically allocated memory or when passing variables by reference. Pointer should be used when you need to store a value of any data type, such as integers, characters, or strings.

What is the purpose of the Member Access Operator in C++?

The Member Access Operator (->) is used to access the members of a class or structure through a pointer to that class or structure. It allows you to access the data members and member functions of an object using a pointer, rather than directly accessing them through the object itself.

How do I use the Member Access Operator in C++?

To use the Member Access Operator in C++, you need to have a pointer to a class or structure. You can then use the arrow (->) operator to access the members of the class or structure through the pointer. For example, if you have a pointer called ptr and a member function called print(), you can access it using ptr->print().

Can I use the Member Access Operator with regular variables in C++?

No, the Member Access Operator can only be used with pointers to classes or structures. It is not possible to use it with regular variables, as they do not have any members to access.

Similar threads

  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
5
Views
888
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
26
Views
3K
  • Programming and Computer Science
Replies
23
Views
2K
Back
Top