Pointers – Introduction
What is a Pointer?
A pointer is a variable that stores the memory address of another variable. Instead of holding a data value (like 42 or 3.14), it holds the location in memory where a value is stored.
Every variable you create occupies some memory. That memory has an address (a number). A pointer stores that address.
int x = 42; // x stores value 42, at some address like 0x1234 int *p = &x; // p stores the ADDRESS of x (0x1234) // ↑ means "pointer to int" // ↑ means "address of x"
Why use pointers? They allow:
- Pass by reference — modify variables in other functions
- Dynamic memory — allocate memory at runtime (malloc, free)
- Arrays and strings — efficient array manipulation
- Data structures — linked lists, trees, graphs
Declaring and Initializing Pointers
int *ip; // Pointer to int float *fp; // Pointer to float char *cp; // Pointer to char int *np = NULL; // NULL pointer — points to nothing (safe!) // Good initialization: int age = 25; int *ptr = &age; // ptr points to age
An uninitialized pointer contains a random (garbage) address. Dereferencing it causes undefined behavior — program crash! Always initialize to NULL or a valid address before use.
& and * Operators
| Operator | Name | Usage | Returns |
|---|---|---|---|
& | Address-of | &variable | Memory address of variable |
* | Dereference (Indirection) | *pointer | Value at the address stored in pointer |
#include <stdio.h> int main() { int x = 42; int *p = &x; // p points to x printf("Value of x = %d\\n", x); // 42 printf("Address of x = %p\\n", &x); // 0x... (hex address) printf("Value of p = %p\\n", p); // Same as &x printf("*p (value) = %d\\n", *p); // 42 (dereferencing) *p = 100; // Change x through pointer! printf("x after *p=100: %d\\n", x); // x is now 100! return 0; }
Pointer Arithmetic
You can add or subtract integers from pointers. The pointer moves by the size of its type.
int arr[] = {10, 20, 30, 40, 50}; int *p = arr; // Points to first element printf("%d\\n", *p); // 10 p++; // Move to next int (+4 bytes) printf("%d\\n", *p); // 20 p += 2; // Move 2 ints forward (+8 bytes) printf("%d\\n", *p); // 40
Pointers in Functions – Pass by Reference
#include <stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 10, y = 20; printf("Before: x=%d, y=%d\\n", x, y); swap(&x, &y); // Pass addresses printf("After: x=%d, y=%d\\n", x, y); return 0; }
Pointers and Arrays
An array name itself is a pointer to its first element!
int arr[] = {1, 2, 3}; // These are equivalent: printf("%d\\n", arr[2]); // Array notation printf("%d\\n", *(arr + 2)); // Pointer notation
Summary
- A pointer stores the memory address of a variable
- Declare with
*:int *p; &variable— gets address of variable*pointer— gets value at the address (dereference)- Always initialize pointers before use; use
NULLas safe default - Pointer arithmetic moves by the size of the pointed type
- Array name = pointer to first element
- Pass pointers to functions to modify variables (pass by reference)
Write programs: (1) Print address and value of 5 variables (2) Swap two numbers using pointers (3) Traverse array using pointer arithmetic (4) Function that returns min and max of array via pointer params.
Frequently Asked Questions
What is a pointer in C?
How do you declare a pointer in C?
* before the name in the declaration, such as int *p;, which means "p is a pointer to an int." You then make it point to a variable with the address-of operator, like p = &x;.What do the * and & operators do with pointers?
& operator gives the address of a variable, so &x is where x lives in memory. The * operator dereferences a pointer, so *p reads or changes the value at the address the pointer holds.What is a NULL pointer in C?
NULL. It is used to show that a pointer is not currently pointing at valid data, and you should always check for NULL before dereferencing a pointer.