Tuesday, 24 March 2020

C++ - Arrow Vs Dot


The dot operator is applied to the actual object. The arrow operator is used with a pointer to an object.
struct Student {
   char first_name[16];
   int  age;
}  emp;

The (.) dot operator
To assign the value "lala" to the first_name member of object emp, you would write something as follows −
strcpy(std.first_name, "lala");

The (->) arrow operator
If p_emp is a pointer to an object of type Employee, then to assign the value "zara" to the first_name member of object emp, you would write something as follows −
strcpy(p_std->first_name, "lala");
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.

No comments:

Post a Comment