SoFunction
Updated on 2025-03-10

Explain the role and usage of the Address-of operator & in C++ programming

grammar

& cast-expression

Remark
The unary address-of operator (&) takes the address of its operand. The operand of the address-of operator can be a function indicator or an lvalue that specifies an object that is not a bitfield and does not use the register storage class specifier.
The address-of operator is only applicable to variables with base, structure, class, or union type declared at the file scope level, or only for subscript array references. Among these expressions, constant expressions that do not include the address-of operator can be added or extracted in the address-of expression.
When applied to a function or lvalue, the result of this expression will be a pointer type derived from the operand type (rvalue). For example, if the operand is of type char, the result of the expression is a type pointer to char. The address-of operator (applied to a const or volatile object) evaluates to const type * or volatile type *, where type is the type of the original object.
When applying the address-of operator to a qualified name, the result will depend on whether qualified-name specifies a static member. If so, the result is a pointer to the type specified in the member declaration. If the member is not static, the result is a pointer to the member name of the class indicated by qualified-class-name.
The following code snippet illustrates the difference in the results, depending on whether the member is static:

// expre_Address_Of_Operator.cpp
// C2440 expected
class PTM {
public:
      int  iValue;
  static float fValue;
};

int main() {
  int  PTM::*piValue = &PTM::iValue; // OK: non-static
  float PTM::*pfValue = &PTM::fValue; // C2440 error: static
  float *spfValue   = &PTM::fValue; // OK
}

In this example, since fValue is a static member, the expression &PTM::fValue produces type float * instead of type float PTM::*.
The address of the overloaded function can only be used if the version of the function to be referenced is explicitly specified. For information on how to get the address of a specific overloaded function, see Address of an overloaded function.
By applying the address-of operator to a reference type, you can obtain the same result as you get from applying the operator to the object you referenced to. For example:

// expre_Address_Of_Operator2.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main() {
  double d;    // Define an object of type double.
  double& rd = d; // Define a reference to the object.

  // Obtain and compare their addresses
  if( &d == &rd )
   cout << "&d equals &rd" << endl;
}

Output

&d equals &rd

The following example uses the address-of operator to pass pointer parameters to a function:

// expre_Address_Of_Operator3.cpp
// compile with: /EHsc
// Demonstrate address-of operator &

#include <iostream>
using namespace std;

// Function argument is pointer to type int
int square( int *n ) {
  return (*n) * (*n);
}

int main() {
  int mynum = 5;
  cout << square( &mynum ) << endl;  // pass address of int
}

Output

25