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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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