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

AI Thread Summary
In C++, the asterisk (*) indicates a pointer, with its position varying based on context; it appears after the type in declarations (e.g., char* x) and before the variable in expressions (e.g., *x for dereferencing). The member access operator (->) serves as a shorthand for accessing members of an object pointed to by a pointer, simplifying syntax and reducing potential errors associated with parentheses. While the use of -> is not mandatory, it enhances code readability and minimizes ambiguity compared to using the dereference operator and parentheses. Additionally, pointers can be treated like arrays, allowing for alternative ways to dereference or access elements, such as using pointer offsets. Overall, C++ provides multiple methods for pointer manipulation, showcasing its flexibility.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
40
Views
4K
Replies
19
Views
5K
Replies
17
Views
3K
Replies
4
Views
2K
Replies
7
Views
11K
Back
Top