Topics
& Operator
Address Of
Operator (&)
- To get the address of a variable, we can use the address-of operator (&)
- Address will be in Hexadecimal number, i.e.
- Hexadecimal number starts with 0x…
int p=5;
cout << p << endl; // 5
cout << &p << endl; // Address of p
- The & operator returns a pointer to its operand
- It can be applied to variables, arrays, functions, etc.
- Does not allocate new memory, simply returns existing address
Input | Operator | Type | Explanation |
---|---|---|---|
5 & 3 | Bitwise AND | Binary | Performs a bitwise AND operation on the binary representations of 5 and 3 |
&x | Address of | Unary | Returns a pointer to the variable x. This does not perform a bitwise operation. |
The key things to understand: |
- The & operator behaves differently depending on the context
- With two numeric operands (e.g. 5 & 3), it performs a bitwise AND operation
- With a single variable operand (e.g. &x), it returns the memory address of that variable
- Bitwise AND operates on the binary representations of integers
- Compares each bit position and sets to 1 only if both bits are 1
- Address of returns a pointer
- This is a memory address that can be used to access the variable
- Allows passing variables by reference instead of by value
Summary
- & with two numbers → bitwise AND
- & with a variable → address of operator
Example
#include<iostream>
using namespace std;
int main(){
int x = 10;
cout << &x << endl;
// Prints memory address of x in hexadecimal format
float y;
cout << &y << endl;
// Prints memory address of uninitialized float variable y
char letter = 'A';
cout << &letter << endl;
// Prints address of char variable letter
cout << (void *)&letter << endl;
// Casts &letter to a void* pointer
// Allows printing as hexadecimal address instead of char
int arr[100];
cout << arr << endl;
// Prints base address of array arr
// Array name represents address of first element
cout << &arr << endl;
// Prints address of entire array variable
return 0;
}
Key points:
- & operator prints the memory address
- Works for regular variables, uninitialized vars, arrays
- Arrays have a base address that points to the first element
- Char pointers print ASCII character by default, need to cast to print address
Pointers
In C++, a pointer is a variable that holds the address of another variable.
To declare a pointer:
dataType *pointerName;
- dataType is the type of data the pointer points to
- The * indicates it is a pointer variable
int x = 10, y = 20;
// Declare a Pointers
int * xPtr = &x; // A pointer to an integer value
int * yptr; // Declare
yptr = &x; // Assign
Dereference Operator *
An interesting property of pointers is that they can be used to access the variable they point directly. This is done by preceding the pointer name with the dereference operator (*
). The operation itself can be read as value pointed to by”
int x = 10; y = 20;
// Declaring a Pointer
int * xPtr = &x; // a pointer to an integer value
cout << xPtr; // Address of X
cout << *xPtr; // Value of X (Dereference Operator)
Example:
#include<iostream>
using namespace std;
int main(){
int x = 10;
int * xPtr = &x;
cout << x << endl; // 10
cout << xPtr << endl // address
cout << &xPtr << endl; // address of pointer bucket
cout << *xPtr << endl; // x -> 10
cout << *(&x) << endl; // 10
return 0;
}
Null Pointer
A null pointer points to address 0 (nothing):
int x = 10, y = 20;
// Declaring a Pointer
int * xPtr = &x; // a pointer to an integer value
// Setting to NULL
xPtr = 0;
int *p = 0;
int *q = NULL;
- Always check for null before dereferencing
- Dereferencing null pointer causes errors
Double Pointers
C++ allows pointers that point to other pointers, called double pointers:
int **varPtr; // Holds address of another pointer
The rules of dereferencing apply, but an additional *
is required:
cout << **varPtr;
Reference Variables
- References are like constant pointers that are automatically dereferenced
- A reference variable us an alias, that is another name for an already existing value (Not to a new memory)
- Declared using
&
instead of*
int x = 10;
int &xRef = x; // Reference to x
- Once initialized, a reference cannot be changed to reference another variable
- More efficient and easier syntax than pointers in many cases
Example
#include<iostream>
using namespace std;
int main(){
int x = 10;
int &y = x;
cout << x << endl; // 10
cout << y << endl; // 10
return 0;
}
Passing data to functions
There are two main ways to pass data to functions in C++ - by value and by reference:
1. Pass by Value
Function gets a copy of the arguments, does not modify original
void func(int x) {
x++; // local copy only
}
int n = 10;
func(n); // n unchanged
- Changes to parameters not reflected outside
- Okay for basic data types
2. Pass by Reference
Function gets a copy of the arguments, does not modify original
- using pointers
#include<iostream>
using namespace std;
void incMoney(int *moneyPtr){
cout << moneyPtr << endl;
// De-reference Operator
(*moneyPtr) = 2*(*moneyPtr);
}
int main(){
int money = 10;
// Pass by reference using pointer variables
incMoney(&money);
cout << "Main : " << money << endl;
return 0;
}
- using reference variables
NOTE
- More efficient than copying large data
- Allows modifying passed parameters
- Use const to prevent modification
#include<iostream>
using namespace std;
// Pass by reference using Reference Variables
void incMoney(int &myMoney){
myMoney = 2*myMoney;
cout << "My Money : " << myMoney << endl;
}
int main(){
int money = 10;
incMoney(money);
cout << "Main money : " << money << endl;
return 0;
}