Types of Pointers in C
Contents
Types of Pointers in C: Concepts, Examples, and Usage
1. What is a Pointer?
- A pointer is a variable that stores the memory address of another variable.
- Used for dynamic memory, arrays, functions, strings, data structures, and more.
2. Types of Pointers in C
2.1 Null Pointer
- Points to nothing; used to signify “no object” or error.
- Safe value for uninitialized or freed pointers.
- Defined by: `NULL` macro (`#include <stddef.h>`).
int *ptr = NULL;
if (ptr == NULL) { /* handle error */ }
2.2 Void Pointer (Generic Pointer)
- Can point to any data type.
- No associated type; needs casting to dereference.
- Used for generic functions (e.g., `malloc`, library APIs).
void *ptr;
int a = 10;
ptr = &a; // OK
// printf("%d", *ptr); // Error: must cast first
printf("%d", *(int*)ptr);
2.3 Wild (Uninitialized) Pointer
- Declared but not assigned; points to unknown (random) location.
- Dangerous! Using it is undefined behavior.
int *ptr; // wild pointer
*ptr = 5; // undefined, may crash or corrupt data
2.4 Dangling Pointer
- Points to memory that has been freed or gone out of scope.
- Accessing is undefined behavior.
int *ptr = malloc(sizeof(int));
free(ptr); // ptr is now dangling
// *ptr = 42; // undefined, do NOT use!
2.5 Function Pointer
- Stores the address of a function.
- Enables callback, dynamic dispatch, plug-ins.
void hello() { printf("Hello!\n"); }
void (*funcptr)() = hello;
funcptr(); // calls hello()
2.6 Array Pointer (Pointer to Array)
- Points to the first element of an array (not to be confused with “array of pointers”).
int arr[5] = {1,2,3,4,5};
int *p = arr; // points to arr[0]
int (*q)[5] = &arr; // q is a pointer to an array of 5 ints
2.7 Constant Pointer
- The pointer cannot change (address stored is fixed), but the data it points to can.
int x = 10, y = 20;
int *const ptr = &x;
*ptr = 50; // OK
// ptr = &y; // Error: ptr is constant
2.8 Pointer to Constant
- The pointer can change, but the data pointed to cannot (read-only via this pointer).
int x = 10, y = 20;
const int *ptr = &x;
// *ptr = 20; // Error: data is constant
ptr = &y; // OK
2.9 Constant Pointer to Constant
- Both the pointer and the value it points to are constant.
int x = 10;
const int *const ptr = &x;
// *ptr = 20; // Error
// ptr = &y; // Error
2.10 Double Pointer (Pointer to Pointer)
- Stores the address of another pointer.
- Used for dynamic memory (e.g., array of strings), pointer to arrays, or modifying pointer in function.
int x = 10;
int *p = &x;
int **pp = &p;
printf("%d\n", **pp); // prints 10
2.11 Null-terminated Pointer (String Pointer)
- Special case: pointers to characters with ‘\0’ as end, used for strings.
char *str = "hello";
while (*str) { putchar(*str++); }
3. Summary Table
Type | Declaration | Usage |
---|---|---|
Null Pointer | int *p = NULL; | “No object”, error-check |
Void Pointer | void *p; | Generic API |
Wild Pointer | int *p; | Dangerous (uninitialized) |
Dangling Pointer | p = freed_ptr; | Dangerous (freed/out-of-scope) |
Function Pointer | void (*f)(); | Callbacks, dynamic dispatch |
Array Pointer | int (*a)[N]; | Multidimensional arrays |
Constant Pointer | int *const p; | Fixed address |
Pointer to Constant | const int *p; | Read-only via pointer |
Const Ptr to Const | const int *const p; | Read-only, fixed address |
Double Pointer | int **p; | Pointer to pointer |
String Pointer | char *s; | Strings (null-terminated) |