Pointer Declaration in C++
A pointer in C++ is a variable that stores the address of another variable.
1. Syntax of Pointer Declaration
data_type *pointer_name;
2. Examples of Pointer Declaration
(a) Integer Pointer
int *p;
(b) Float Pointer
float *fp;
(c) Character Pointer
char *cp;
3. Initialization of Pointer
int a = 10;
int *p = &a;
Here:
- p stores the address of a
- & → address-of operator
4. Accessing Value Using Pointer (Dereferencing)
cout << *p; // Output: 10
*p gives the value stored at the address.
cout << *p; // Output: 10
*p gives the value stored at the address.
5. Multiple Pointer Declaration
int *p1, *p2;
Note:- Correct way (both are pointers).
6. Null Pointer
int *p = NULL; // or nullptr (modern C++)
7. Pointer to Pointer
int **pp;
Important Points
- * is used in declaration and dereferencing.
- Pointer must be initialized before use.
- Uninitialized pointers cause runtime errors.
Pointer operator in C++
In C++, pointer operators are used to work with memory addresses and values stored at those addresses. There are two main pointer operators.
1. Address-of Operator (&)
Used to get the address of a variable.
Syntax
&variable_name
Example
int a = 10;
int *p = &a;
cout << &a; // prints address of a
2. Dereference (Indirection) Operator (*)
Used to access the value stored at the address held by a pointer.
Syntax
*pointer_name
Example
cout << *p; // Output: 10
Combined Example
#include <iostream>
using namespace std;
int main()
{
int x = 25;
int *p = &x;
cout << "Address of x: " << &x << endl;
cout << "Value using pointer: " << *p << endl;
return 0;
}
Summary Table
Operator Symbol Purpose
Address-of & Gets memory address
Dereference * Gets value at address
Important Points
- * in declaration means pointer variable.
- * in expression means value at address.
- Pointer must point to a valid variable before dereferencing.
- Using uninitialized pointer causes undefined behavior.
Address operator in C++
The address operator (&) in C++ is used to find the memory address of a variable.
Definition
The address operator (&) returns the address (location) in memory where a variable is stored.
Syntax
&variable_name
Example
#include <iostream>
using namespace std;
int main()
{
int a = 10;
cout << &a;
return 0;
}
Using Address Operator with Pointer
int a = 25;
int *p = &a;
cout << p; // prints address of a
Key Points
- & is a unary operator.
- It works only with variables, not constants.
- Commonly used to initialize pointers.
- Returns an address, not a value.
Invalid Usage
cout << &10; // ❌ Error (constant has no address)
Exam-Oriented Definition
Address Operator: The operator & used to obtain the memory address of a variable is called the address operator in C++.
Pointer expressions in C++
Pointer expressions in C++ are expressions that involve pointer variables and are used to access, manipulate, or compare memory addresses and the values stored at those addresses.
1. Basic Pointer Expression
*p
Gives the value stored at the address pointed to by p.
2. Assignment Expression
p = &a;
Assigns the address of a to pointer p.
3. Pointer Arithmetic Expressions
(a) Increment Pointer
p++;
Moves pointer to next memory location of same data type.
(b) Decrement Pointer
p--;
(c) Add / Subtract Integer
p = p + 2;
p = p - 1;
4. Pointer and Array Expression
*(p + i)
Equivalent to:
a[i]
5. Pointer Comparison
if(p1 == p2)
Compares addresses, not values.
6. Pointer to Pointer Expression
**pp
Accesses value using double pointer.
7. Example Program
#include <iostream>
using namespace std;
int main()
{
int a[3] = {10, 20, 30};
int *p = a;
cout << *p << endl; // 10
cout << *(p + 1) << endl; // 20
cout << *(p + 2) << endl; // 30
return 0;
}
Important Points
- Pointer arithmetic depends on data type size.
- *(p + i) is same as p[i].
- Invalid pointer expressions cause undefined behavior.
- Do not dereference NULL or uninitialized pointers.
Exam-Oriented Definition
Pointer Expressions: Expressions involving pointer variables and pointer operators used to access or manipulate memory locations are called pointer expressions in C++.
Pointer Arithmetic in C++
Pointer arithmetic refers to performing arithmetic operations on pointers. In C++, pointer arithmetic is mainly used to access array elements efficiently.
1. Why Pointer Arithmetic is Needed
Pointers store addresses. When we increment or decrement a pointer, it moves by the size of the data type it points to.
Example:
- int → 4 bytes
- char → 1 byte
- double → 8 bytes
2. Valid Pointer Arithmetic Operations
(a) Increment (++)
Moves pointer to the next element.
int a[3] = {10, 20, 30};
int *p = a;
p++; // moves to next int element
(b) Decrement (--)
Moves pointer to the previous element.
p--; // moves back to previous element
(c) Addition (+)
Moves pointer forward by a given number of elements.
p = p + 2; // moves two elements ahead
(d) Subtraction (-)
Moves pointer backward.
p = p - 1; // moves one element back
(e) Difference of Two Pointers
Gives the number of elements between them (not bytes).
int *p1 = &a[0];
int *p2 = &a[2];
int diff = p2 - p1; // diff = 2
3. Pointer Arithmetic with Arrays
Array name acts as a constant pointer to the first element.
int a[5] = {1, 2, 3, 4, 5};
cout << *(a + 2); // Output: 3
Explanation:
a + 2 → address of 3rd element
*(a + 2) → value at that address
4. Pointer Comparison
Pointers can be compared using relational operators.
if (p1 < p2)
cout << "p1 points to lower address";
5. Invalid Pointer Operations ❌
These are not allowed:
Adding two pointers
p1 + p2; // ❌ invalid
Multiplying pointers
p1 * p2; // ❌ invalid
Arithmetic on void* pointers (without casting)
6. Example Program
#include <iostream>
using namespace std;
int main() {
int a[3] = {10, 20, 30};
int *p = a;
cout << *p << endl; // 10
p++;
cout << *p << endl; // 20
p++;
cout << *p << endl; // 30
return 0;
}
7. Key Points (Exam-Oriented)
- Pointer arithmetic depends on data type size
- Used mainly with arrays
- p++ moves to next element, not next byte
- Only increment, decrement, addition, subtraction, comparison are allowed

0 comments
Post a Comment