Special Types of Pointers in C
Most pointer crashes come from pointers that point to the wrong place: nowhere, somewhere random, or somewhere that no longer exists. These "special" pointers have names: NULL, wild and dangling. A fourth, the void pointer, is not a bug at all but a generic pointer that can hold any address.
Interviewers ask about these four all the time. In this lesson you will learn what each one is, how it happens, and the simple habits that prevent pointer bugs.
What You Will Learn
- What a NULL pointer is and how to use it safely.
- What a void (generic) pointer is.
- What a wild pointer is.
- What a dangling pointer is and its three causes.
- Safe pointer habits.
- A note on near, far and huge pointers.
Special Types of Pointers Explained
| Pointer | Exam definition | Safe? |
|---|---|---|
| NULL pointer | A pointer assigned NULL; it points to nothing. | Safe to check, not to dereference |
| void pointer | A generic pointer (void *) that can hold the address of any type; must be cast before dereferencing. | Safe when cast correctly |
| Wild pointer | A pointer that is declared but never initialized; it holds a random address. | Dangerous |
| Dangling pointer | A pointer that points to memory that has been freed or destroyed. | Dangerous |
Memory trick: Think of addresses on paper. NULL = a slip that says "no address" (honest). Wild = a slip with a random scribble (never filled in). Dangling = the address of a house that was demolished. void = an address with no label of what's inside; you must say what it is before going in.
NULL Pointer
int *p = NULL; /* points to nothing */
if (p != NULL) /* always check before using */
printf("%d", *p);
NULLis a macro defined instdio.h,stdlib.handstddef.h; it is usually((void *)0).- Functions return NULL to say "failed / not found":
malloc,fopen,strchr,strstr. - Dereferencing NULL (
*pwhen p is NULL) usually crashes with a segmentation fault, which at least makes the bug easy to notice. - A null pointer (points nowhere) is different from the null character
'\0'(ends a string).
void Pointer (Generic Pointer)
int n = 10;
float f = 2.5f;
void *vp;
vp = &n; /* any address is allowed */
printf("%d", *(int *) vp); /* cast, then dereference */
vp = &f;
printf("%.1f", *(float *) vp);
- A
void *doesn't know the type, so*vpis an error. Cast it first:*(int *) vp. - Pointer arithmetic on
void *is not allowed in standard C (the step size is unknown). mallocreturnsvoid *, andqsortpassesconst void *to your compare function. That's how they work with any type.
Example 1: NULL Checks and a Generic void Pointer Function
#include <stdio.h>
#include <string.h>
void print_value(const void *data, char type) /* works for any type */
{
if (data == NULL)
{
printf("(nothing to print)\n");
return;
}
switch (type)
{
case 'i': printf("int : %d\n", *(const int *) data); break;
case 'f': printf("float : %.2f\n", *(const float *) data); break;
case 'c': printf("char : %c\n", *(const char *) data); break;
case 's': printf("string : %s\n", (const char *) data); break;
}
}
int main(void)
{
int roll = 42;
float cgpa = 8.75f;
char grade = 'A';
char name[] = "Priya";
char *found;
print_value(&roll, 'i');
print_value(&cgpa, 'f');
print_value(&grade, 'c');
print_value(name, 's');
print_value(NULL, 'i');
found = strchr(name, 'z'); /* returns NULL: not found */
if (found == NULL)
printf("'z' is not in %s\n", name);
return 0;
}
Output
int : 42 float : 8.75 char : A string : Priya (nothing to print) 'z' is not in Priya
Code Explanation
const void *dataaccepts the address of any type. Thetypeletter tells the function how to read it.- Each
casecasts the void pointer to the right type, then dereferences it. - The first thing the function does is a NULL check, so passing NULL is harmless.
strchrreturns NULL when the character isn't found, so the caller checks before using the result.
Wild Pointer
int *p; /* wild: holds a random address */
*p = 25; /* writes to an unknown place: crash or silent corruption */
- Local pointers are not set to anything automatically, so they start wild.
- Fix: always initialize:
int *p = NULL;orint *p = &x;. - A wild pointer is worse than a NULL pointer: it may "work" in testing and fail later.
Dangling Pointer: Three Common Causes
/* 1. Returning the address of a local variable */
int *make(void) { int x = 5; return &x; } /* x dies when make() ends */
/* 2. Using memory after free() */
int *p = malloc(sizeof(int));
free(p); /* memory given back */
*p = 10; /* dangling: use after free */
/* 3. Variable goes out of scope */
int *q;
{
int y = 7;
q = &y;
} /* y no longer exists */
printf("%d", *q); /* dangling */
Fix: after free(p), write p = NULL;. Never return or keep the address of a local variable after its block ends.
Example 2: Avoiding a Dangling Pointer After free
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *marks = malloc(3 * sizeof(int)); /* ask for 3 ints */
if (marks == NULL) /* malloc can fail */
{
printf("Memory not available\n");
return 1;
}
marks[0] = 70; marks[1] = 82; marks[2] = 91;
printf("Marks: %d %d %d\n", marks[0], marks[1], marks[2]);
free(marks); /* give the memory back */
marks = NULL; /* no longer dangling */
if (marks == NULL)
printf("Pointer reset to NULL after free: safe\n");
return 0;
}
Output
Marks: 70 82 91 Pointer reset to NULL after free: safe
mallocreturnsvoid *; in C it converts toint *automatically. You will study it fully in Module 12.- After
free, the pointer still holds the old address. Setting it toNULLturns a dangerous dangling pointer into a safe, checkable NULL pointer.
Near, Far and Huge Pointers (Old Exam Topic)
Some syllabi still ask about these. They existed only in 16-bit DOS compilers like Turbo C, where memory was split into 64 KB segments:
- Near pointer: 16-bit, reaches only the current 64 KB segment.
- Far pointer: 32-bit (segment + offset), reaches other segments.
- Huge pointer: like far, but normalized so arithmetic works across segments.
Modern compilers (GCC, Clang, MSVC) on 32/64-bit systems use a flat memory model and don't support these keywords.
Important Points to Remember
- NULL pointer = points to nothing; check before use.
- void * = generic; cast before dereferencing; no arithmetic.
- Wild = uninitialized; fix by always initializing.
- Dangling = points to freed/destroyed memory; fix by p = NULL after free and not returning local addresses.
- malloc, fopen, strchr return NULL on failure.
- Near/far/huge are obsolete (Turbo C only).
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
int *p; *p = 1; | Wild pointer write: crash/corruption | Initialize p |
Using a pointer after free | Dangling: random results | p = NULL; after free |
*vp on a void * | Compile error | *(int *) vp |
| Not checking malloc/fopen result | NULL dereference: crash | if (p == NULL) |
Confusing NULL with '\0' | Wrong comparisons | NULL for pointers, '\0' for chars |
Special Pointers Practice Questions
- What is the difference between a NULL pointer and a wild pointer?
- Why can't a void pointer be dereferenced directly?
- Give three ways a dangling pointer is created.
- How do you make a pointer safe after calling
free? - Which standard functions return NULL on failure? Name two.
Show answers
- A NULL pointer deliberately points to nothing and can be checked; a wild pointer was never initialized and holds a random address.
- The compiler doesn't know the type, so it doesn't know how many bytes to read or how to interpret them.
- Returning the address of a local variable, using memory after free, and keeping the address of a variable that went out of scope.
- Set it to NULL right after free.
- malloc, fopen (also strchr, strstr, calloc).
Coding Exercise
Write a generic function void swap_any(void *a, void *b, size_t size) that swaps two values of any type byte by byte (hint: treat them as unsigned char *). Test it with two ints and two doubles.
Show solution
#include <stdio.h>
void swap_any(void *a, void *b, size_t size)
{
unsigned char *p = a, *q = b, temp;
size_t i;
for (i = 0; i < size; i++)
{
temp = p[i];
p[i] = q[i];
q[i] = temp;
}
}
int main(void)
{
int x = 3, y = 9;
double d1 = 1.5, d2 = 7.25;
swap_any(&x, &y, sizeof(int));
swap_any(&d1, &d2, sizeof(double));
printf("x = %d, y = %d\n", x, y);
printf("d1 = %.2f, d2 = %.2f\n", d1, d2);
return 0;
}
x = 9, y = 3 d1 = 7.25, d2 = 1.50
Quick Quiz: NULL, void, Wild and Dangling Pointers (MCQ)
-
A pointer that is declared but not initialized is a:
A) NULL pointer B) Wild pointer C) void pointer D) Function pointer
-
A pointer to memory that has been freed is a:
A) Dangling pointer B) NULL pointer C) Generic pointer D) Far pointer
-
Which pointer can hold the address of any data type?
A)
int *B)char *C)void *D)NULL -
Correct way to read an int through
void *vp:A)
*vpB)(int) vpC)*(int *) vpD)&vp -
Best practice right after
free(p);:A)
*p = 0;B)p = NULL;C)free(p);again D) Nothing
Show answers
- B: wild pointer.
- A: dangling pointer.
- C:
void *. - C:
*(int *) vp. - B:
p = NULL;
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| NULL pointer | Points to nothing; check with p == NULL |
|---|---|
| void pointer | Generic; cast before *; no arithmetic |
| Wild pointer | Uninitialized, random address |
| Dangling pointer | Points to freed / destroyed memory |
| Prevent wild | Initialize every pointer |
| Prevent dangling | p = NULL after free; don't return locals |
| Near/far/huge | Turbo C only, obsolete |
Frequently asked questions
- What is the difference between a NULL pointer and a void pointer?
- NULL is a value meaning "points nowhere"; void * is a type that can point to any type.
- What is a dangling pointer and how do you avoid it?
- A pointer to memory that no longer exists; avoid it by setting p = NULL after free and never returning local addresses.
- What is a wild pointer?
- A pointer that was never initialized, so it holds a garbage address.
Remember
- NULL = "no address", wild = scribble, dangling = demolished house, void = unlabelled box.
- Initialize → check → free → NULL.
Next Topic
Next lesson: const and Pointers in C. You will combine const with pointers and learn to read declarations like const int *p and int *const p.