What is a Pointer?
A pointer is a special variable that stores the memory address of another variable instead of storing the actual value.
Definition:
A pointer in C is a variable that stores the memory address of another variable.
Example
#include <stdio.h>
int main()
{
int a = 10; // Normal integer variable
int *p; // Pointer variable
p = &a; // Store the address of 'a' in pointer 'p'
printf("Value of a = %d\n", a);
printf("Address of a = %p\n", (void*)&a);
printf("Value stored in p = %p\n", (void*)p);
printf("Value pointed by p = %d\n", *p);
return 0;
}
Output (Example)
Value of a = 10
Address of a = 0x7ffeefbff5ac
Value stored in p = 0x7ffeefbff5ac
Value pointed by p = 10
Note: The memory address will be different every time you run the program.
Step-by-Step Explanation
Step 1: Declare a normal variable
int a = 10;
- Variable name:
a - Value:
10 - It occupies a memory location (address).
Step 2: Declare a pointer
int *p;
pis a pointer variable.*indicates thatpis a pointer.intmeans it can store the address of an integer variable.
Step 3: Store the address
p = &a;
&ameans address ofa.- This address is stored in pointer
p.
Step 4: Access the value
*p
The * operator is called the dereference operator.
It means:
Go to the address stored in
pand retrieve the value stored there.
If:
a = 10
Address of a = 1000
p = 1000
Then:
*p = 10
Memory Diagram
Memory
Address Value
----------------------
1000 10
โ
|
p
Or more clearly:
+-----------+ +-----------+
| p | -----> | a |
| Address | | Value=10 |
+-----------+ +-----------+
Changing the Value Using a Pointer
#include <stdio.h>
int main()
{
int a = 10;
int *p = &a;
*p = 50;
printf("a = %d", a);
return 0;
}
Output
a = 50
Explanation:
pstores the address ofa.*p = 50changes the value stored at that address.- Therefore, the value of
abecomes50.
Important Operators
| Operator | Meaning | Example |
|---|---|---|
& | Address-of operator | &a gives the address of a |
* | Dereference operator | *p gives the value stored at the address |
Quick Summary
| Expression | Meaning |
|---|---|
a | Value of the variable |
&a | Address of the variable |
p | Address stored in the pointer |
*p | Value stored at that address |
Real-Life Analogy
Imagine a house:
- House โ Variable (
a) - House Address โ Memory address (
&a) - Paper containing the house address โ Pointer (
p) - Going to the address and entering the house โ Dereferencing (
*p)
A pointer doesn’t store the house itself; it stores where the house is located.
Exam Definition
A pointer is a variable that stores the memory address of another variable. It allows indirect access to the value stored at that memory location.
Category: MCA
