What is the meaning of '&' in c++
Reference Operator: When used as a reference operator, the &
symbol is used to create a reference to a variable. A reference is an alias or an alternate name for an existing variable. It allows you to access and modify the original variable using the reference. References are often used as function parameters for "call by reference."
example :
int x = 10;
int& refX = x; // Creating a reference to variable x
refX = 20; // Modifying the original variable x using the reference
Address-of Operator: When used as an address-of operator, the &
symbol is used to obtain the memory address of a variable. This is useful when working with pointers or when you need to pass the memory address of a variable to a function.
Example:
int y = 5;
int* ptrY = &y; // Getting the memory address of variable y
Bitwise AND Operator: The &
symbol is also used as a bitwise AND operator in C++. It performs bitwise AND operation on individual bits of two integers.
Example:
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a & b; // Binary AND: 0001 (Decimal: 1)