C++ Member (dot & arrow) Operators

« Previous Chapter Next Chapter »

The . (dot) operator and the -> (arrow) operator are used to reference individual members of classes, structures, and unions.

The dot operator is applied to the actual object. The arrow operator is used with a pointer to an object. For example, consider the following structure:

Example

struct Friend {
  char first_name[16];
  int  age;
} emp;
                                        

The (.) dot operator:

To assign the value "Sophia" to the first_name member of object emp, you would write something as follows:

Example

strcpy(emp.first_name, "Sophia");
                                        

The (->) arrow operator:

If p_frnd is a pointer to an object of type Friend, then to assign the value "Sophia" to the first_name member of object emp, you would write something as follows:

Example

strcpy(emp->first_name, "Sophia");
                                        

The -> is called the arrow operator. It is formed by using the minus sign followed by a greater than sign.

Simply saying: To access members of a structure, use the dot operator. To access members of a structure through a pointer, use the arrow operator.


« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!