A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cplusplus/cpp_member_operators.htm below:

C++ Member (dot & arrow) Operators

C++ Member (dot & arrow) Operators

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 −

struct Employee {
   char first_name[16];
   int  age;
}  emp;
The (.) dot operator

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

strcpy(emp.first_name, "zara");
Example
#include <iostream>
#include <cstring>
using namespace std;

struct Employee {
    char first_name[20];
};

int main() {
    Employee emp;

    // Using dot operator to assign a value
    strcpy(emp.first_name, "zara");

    cout << "First Name: " << emp.first_name << endl;

    return 0;
}

When executed, this program outputs:

First Name: zara
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_emp->first_name, "zara");

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

Example
#include <iostream>
#include <cstring>
using namespace std;

struct Employee {
    char first_name[20];
};

int main() {
    Employee emp;
    Employee* p_emp = &emp;

    // Using arrow operator to assign a value
    strcpy(p_emp->first_name, "zara");

    cout << "First Name: " << p_emp->first_name << endl;

    return 0;
}

When executed, this program outputs:

First Name: zara

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.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4