Ora

What is the * operator called in C++?

Published in C++ Operators 1 min read

In C++, the * operator has multiple significant roles, primarily functioning as a multiplication operator and an indirection (or dereference) operator for pointers, as well as being part of pointer declaration syntax. Understanding its context is key to identifying its specific name and function.

The * Operator as a Multiplication Operator

When used in arithmetic expressions, the * symbol is known as the multiplication operator. It performs the mathematical operation of multiplying two numerical values.

Here's a breakdown of common C++ arithmetic operators, including multiplication:

Operator Name Description Example Result
+ Addition Adds together two values 5 + 3 8
- Subtraction Subtracts one value from another 10 - 4 6
* Multiplication Multiplies two values 7 * 2 14
/ Division Divides one value by another 15 / 3 5
% Modulo Returns the division remainder 10 % 3 1

Example of Multiplication:

#include <iostream>

int main() {
    int num1 = 12;
    int num2 = 5;
    int product = num1 * num2; // Here, '*' is the multiplication operator
    std::cout << "The product is: " << product << std::endl; // Output: The product is: 60
    return 0;
}

The * Operator for Pointers

Beyond arithmetic, the * operator plays a crucial role in C++'s pointer system, where its meaning depends on whether it's used in a declaration or an expression. This makes it a highly overloaded operator. For a deeper dive into operators, refer to C++ operators on cppreference.com.

Pointer Declaration

When * is used in a variable declaration, it indicates that the variable being declared is a pointer to a specific data type. In this context, it's often referred to as the pointer declaration operator.

Example of Pointer Declaration:

#include <iostream>

int main() {
    int value = 42;
    int* ptr; // Here, '*' declares 'ptr' as a pointer to an integer
    ptr = &value; // 'ptr' now stores the memory address of 'value'
    std::cout << "Address of value: " << ptr << std::endl;
    return 0;
}

In int* ptr;, * modifies ptr to be of type "pointer to int".

Pointer Dereferencing (Indirection Operator)

When * is used in an expression with an already declared pointer variable, it acts as the dereference operator or indirection operator. Its purpose is to access the value stored at the memory address that the pointer holds.

Example of Pointer Dereferencing:

#include <iostream>

int main() {
    int value = 100;
    int* ptr = &value; // 'ptr' points to 'value'

    // Here, '*' is the dereference operator, accessing the value at 'ptr'
    std::cout << "Value directly: " << value << std::endl;
    std::cout << "Value via pointer (dereferenced): " << *ptr << std::endl; // Output: 100

    *ptr = 200; // Using '*' to modify the value at the memory address 'ptr' points to
    std::cout << "New value directly: " << value << std::endl; // Output: 200
    return 0;
}

In *ptr, * retrieves the int value located at the memory address stored in ptr.

Summary of * Operator Names in C++

  • Multiplication Operator: Used for arithmetic multiplication.
  • Pointer Declaration Operator: Used to declare a variable as a pointer.
  • Dereference Operator (or Indirection Operator): Used to access the value pointed to by a pointer.