Other Operators in C++ ([,],&,*,Sizeof(), [.] and ->)

Other Operators in C++ There are different other operators in C++, which are defined as in below Table –

Other Operators Operator Description Name of the Operators
& used to get the address of the variable. Pointer Operators
* used as pointer to a variable.
Sizeof() gives the size of the variable. sizeof() Operator
, (comma) Used to string together several expressions Comma(,) Operator
.(dot)  operator used to reference individual members of classes, structures, and unions. Member Operators
-> (arrow)
 

The & Operator or “The Address of” Operator or The Reference Operator:

  • The (&) operator is a unary operator.
  • It returns the memory address of its operand.
  • For example, if xyz is an integer variable, (i.e. int xyz;), then &xyz gives its address in the memory.
  • This operator has the same precedence  as the other unary operators and associativity is right-to-left.
  • The & operator can be read as “the address of” which means &xyz will be read as “the address of xyz”.
Example of C++ program for Deference(&) – 
#include<iostream.h>
int main()
  {
  int var=5;
  cout<<"Value = \n"<<var;
  cout<<"Address = "<<&var;  //Notice, the ampersand(&) before var.
  return 0;
  }
Output
Value: 5
Address: 2686778

The Indirection Operator * or The Deference Operator:

  • The second operator is indirection Operator .
  • It is also a unary operator.
  • It returns the value of the variable located at the address specified by its operand.
  • This operator also has the same precedence  as the other unary operators and associativity is right-to-left again.
Example of C++ program for Deference(*) Operator –
#include<iostream.h>
int main()
   {
   int* pc;
   int c;
   c=22;
   cout<<"Address of c = \n" <<c;
   cout<<"Value of c = \n\n"<<c;
   pc=&c;
   cout<<"Address of pointer pc = \n"<<pc;
   cout<<"Content of pointer pc =\n\n"<<*pc;
   c=11;
   cout<<"Address of pointer pc = \n"<<pc;
   cout<<"Content of pointer pc = \n\n"<<*pc;
   *pc=2;
   cout<<"Address of c = \n"<<&c;
   cout<<"Value of c = \n\n"<<c;
   return 0;
   }

Output :
Address of c = 2686784
Value of c = 22

Address of pointer pc = 2686784
Content of pointer pc = 22

Address of pointer pc = 2686784
Content of pointer pc = 11

Address of c = 2686784
Value of c = 2

Sizeof() operator-

This gives the size of variable.It is a unary compile time operator that returns the length (in bytes) of the variable or parenthesized type specifier that it proceeds. Thus it can be used in two forms: syntax: 1. Sizeof (var)  or sizeof var      //where var is a declared variable 2. Sizeof(type)                            //where type is a c++ data type The type name must be enclosed in parenthesis but it is not necessary for variable names. Example of C++ program for Sizeof Operator –
#include <iostream.h>
int main()
    {
    int a;
    float b;
    double c;
    char d;
    cout<<"Size of int = "<<sizeof(a)) <<"bytes";
    cout<<"Size of float="<<sizeof(b))<<"bytes";
    cout<<"Size of double="<<sizeof(c))<<"bytes";
    cout<<"Size of char=",sizeof(d))<<"bytes";
    return 0;
    }
Output :
Size of int=4 bytes
Size of float=4 bytes
Size of double=8 bytes
Size of char=1 byte

The comma operator(,)-

The comma operator is used to string together several expressions. The group of expressions seperated by commas(,) is evaluated left to right in sequence and the result of rightmost expression becomes the value of the total comma-seperated expression. For example, b=(a=3, a+1); first assigns a value 3 and then assigns b the value a+1 i.e. 4. The parenthesis are necessary because the comma operator has a lower precedence than the assignment operator.

The (.) dot operator:

  • The (.) dot operator is used for direct member access.
  • The dot operator is applied to the actual object.~
  • The Dot (.) operator can’t be overloaded, arrow (->) operator can be overloaded.
  • The Dot (.) operator can’t be applied to pointers.

The (->) arrow operator:

  • The arrow de-references a pointer so you can access the object/memory it is pointing to.
  • The arrow operator is used with a pointer to an object.
  • arrow (->) operator can be overloaded.
Example of C++ program for (.) dot and (->) arrow Operator –
#include <iostream.h>
class abcd
   {
   public:
       int number;
       void Create()
          {
          cout << "abcd created, number is:  " << number << "\n" ;
          }
   };

int main()
   {
   abcd x;          // declares x to be a abcd object value,
                   // initialized using the default constructor
   x.number = 420;
   x.Create();
   Car *y;         // declare y as a pointer which points to a Car object
   y = &x;         // assign x's address to the pointer y
   (*y).Create();  // *y is object
   y->Create();    // same as previous line, y points to x object. It
                      stores a reference(memory address) to x object.
   y->number = 555; // this is equal to (*y).number = 456;
   y->Create();
   return 0;
   }
Output : 
Car created, number is:  420
Car created, number is:  420
Car created, number is:  420
Car created, number is:  555
 ]]>

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top