1. Pointers & References
Introduction to Pointers
A pointer is a variable that stores the Memory Address of another variable instead of holding traditional values.
Assignment of Address
The Address-of Operator (&) is used to get the physical memory address of an initialized variable.
int a = 10;
int *p = &a; // p points to 'a'
Indirection / Dereferencing Operator (*)
The Asterisk operator retrieves or alters the value exactly at a particular memory address.
int value = *p; // Reads whatever 'p' holds
Modifying Values Explicitly Using Pointers
*p = 50; // Directly replaces the value in variable 'a'
Difference between Ordinary Variables and Pointers
| Field | Ordinary Variable | Pointer Variable |
|---|---|---|
| Data Bound | Stores mathematical, raw alphanumeric values. | Stores Hexadecimal physical RAM locations. |
| Logic | Read immediately directly. | Requires indirection/dereferencing. |
| Math Scope | General mathematical algebra. | Pointer algorithmic math. |
Pointers & Arrays
The core connection between arrays and pointers is that the specific name of an array inherently automatically acts as a constant pointer holding the exact base address.
int arr[5] = {10, 20, 30, 40, 50};
// 'arr' evaluates implicitly to '&arr[0]'
Accessing generic elements via implicit pointer bindings:
arr[i]is fully equivalent internally to*(arr + i)&arr[i]is essentially functionally exactly(arr + i)
Function Pointers
A pointer doesn't strictly have to point only to variables; it can point physically to structurally compiled functions.
void hello() { printf("Hello"); }
int main() {
// Declarative initialization of function pointer
void (*p)() = hello;
// Explicit dynamic execution natively
p();
return 0;
}
Null Pointer
A pointer that evaluates to nothing and points nowhere explicitly:
int *p = NULL; // Safe initialization technique.
Pointer to Pointer (Double Pointer)
A distinct pointer seamlessly storing the physical address of another pointer variable.
Syntax utilizing a Double Asterisk (**):
int x = 10;
int *p = &x; // Pointer targeting Integer
int **ptr2 = &p; // Pointer explicitly targeting another Pointer
To alter the root integer structure, you execute **ptr2 = 20;.