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++.

0 comments
Post a Comment