Pointer – Definition
A pointer is a variable that stores the memory address of another variable instead of storing a direct value.
Example:
int a = 10;
int *p = &a;
Here:
Addressing Mechanism in C
The addressing mechanism refers to how C programs access memory locations using addresses and pointers.
C supports direct addressing and indirect addressing.
Direct Addressing
Example:
int a = 5;
printf("%d", a);
Indirect Addressing
Example:
int a = 5;
int *p = &a;
printf("%d", *p);
Here:
Pointer Operators
1. Address-of Operator (&)
Example:
int a = 10;
printf("%p", &a);
2. Dereference Operator (*)
Example:
int *p;
*p = 20;
Types of Pointers
1. Integer Pointer
int *p;
2. Pointer to Pointer
Example:
int **pp;
3. Null Pointer
Example:
int *p = NULL;
4. Void Pointer
Example:
void *p;
5. Dangling Pointer
Example:
free(p); // p becomes dangling
Pointer Arithmetic
Pointer arithmetic depends on the data type size.
Example:
int *p;
p = p + 1; // moves by sizeof(int)
Relationship Between Arrays and Pointers
Example:
int a[3] = {10, 20, 30};
int *p = a;
Access:
*(p + 1) = 20;
Advantages of Pointers
-
Efficient memory access
-
Used in dynamic memory allocation
-
Essential for data structures
-
Enables function call by reference
Disadvantages of Pointers