-3

can someone please explain what -> operator does in C++?

Example below:

struct X{ int a[sz];}

void print(X* x){
for(int i =0; i<sz; i++){
    cout << x->a[i] << ' '; // -> is like dereference??

My guess is that it assigns index i in array a with the value of the object that x is pointed to.

SeekingAlpha
  • 7,489
  • 12
  • 35
  • 44
  • 1
    You could look at a precedence table and search it by name. There are many, many explanations on the Internet and in any C++ book that dares call itself that, and it has been asked quite a few times on here. – chris Jul 10 '13 at 10:19
  • There is no assignment going on. You should read an introductory C++ book. – juanchopanza Jul 10 '13 at 10:20

2 Answers2

2

It is a member operator which is used to reference individual members of classes, structures, and unions. Unlike the '.' operator in Java, in C++ it is used only for Object pointers.

NREZ
  • 942
  • 9
  • 13
Hitesh Vaghani
  • 1,342
  • 12
  • 31
  • I see, I come from a Java background and have only begun C++ 3 days ago. So the -> operator is used as a way to access a class's members in java? In java it would be like SomeClass.intMember .... In C++ it would be SomeClass->intMember .... – SeekingAlpha Jul 10 '13 at 10:28
  • @MarcoSusilo, In C++ `.` is for objects, `->` is for pointers. It's just a syntax sugar. You could rewrite `ptr -> member` as `(*ptr).member`. – awesoon Jul 10 '13 at 10:33
0

a is a member element of struct X. -> operator is used to access members from a pointer to structure. I'd recommend that you pick up a good book on C/C++ and read about structures.

patentfox
  • 1,436
  • 2
  • 13
  • 28