C Tutorial
Estimated reading: 5 minutes 256 views

C Pointers โ€“ Mastering Address-Based Programming in C


Introduction โ€“ Why Pointers Are Core to C Programming

Pointers are one of Cโ€™s most powerful and flexible features. They allow you to work directly with memory addresses, enabling performance optimization, efficient data manipulation, and dynamic memory allocation. Mastering pointers is essential for any serious C programmer.

In this guide, youโ€™ll learn:

  • What pointers are and how they enable address-level programming
  • The relationship between pointers, arrays, and structures
  • How to use pointers in functions, dynamic memory, and advanced data structures
  • Key safety concepts like NULL, void, and dangling pointers

Topics Covered

Topic Description
C Pointers OverviewBasic concept and purpose of pointers
C Pointers and ArraysHow arrays and pointers relate in memory
C Pointer ArithmeticNavigating memory using pointer operations
C Pointer to PointerMulti-level referencing with pointer-to-pointer
C Array of PointersArrays containing memory addresses
C Pointer to ArrayReferencing an entire array with a pointer
C Pointers to StructuresAccessing struct members using pointers
C Pointers with FunctionsPassing and returning pointers in functions
C NULL PointerSafe default pointer to no location
๐Ÿ”ณ C void PointerType-agnostic pointer for generic use
๐Ÿšจ C Dangling PointerPointers to freed memory and how to avoid them
โœด๏ธ C DereferencingAccessing values stored at pointer addresses
C Pointer vs ArrayDifferences in structure and memory behavior
Near, Far, and Huge PointersLegacy pointer types in 16-bit and embedded systems

C Pointers Overview

A pointer in C is a variable that stores the memory address of another variable. Pointers enable indirect access to data, making them essential for:

  • Dynamic memory management
  • Function argument manipulation
  • Building data structures like linked lists

Syntax Example:

int x = 10;
int *ptr = &x;
printf("%d", *ptr);  // Output: 10

C Pointers and Arrays

In C, an array name behaves like a constant pointer to its first element. This tight coupling allows:

  • Pointer-style access to array elements
  • Efficient iteration using pointer arithmetic

Example:

int arr[] = {10, 20, 30};
int *p = arr;
printf("%d", *(p + 1));  // Output: 20

C Pointer Arithmetic

Pointers can be incremented or decremented to move across memory addresses based on data type size.

Operations Supported:

  • ptr + 1 โ†’ Move to next element
  • ptr - 1 โ†’ Move to previous element
  • ptr2 - ptr1 โ†’ Difference between two pointers (in elements)

Example:

int arr[] = {1, 2, 3};
int *p = arr;
p++;
printf("%d", *p);  // Output: 2

C Pointer to Pointer

A pointer to pointer stores the address of another pointer, allowing multi-level indirection.

Use Case:

  • Dynamic 2D arrays
  • Function arguments like char **argv

Example:

int x = 5;
int *p = &x;
int **pp = &p;
printf("%d", **pp);  // Output: 5

C Array of Pointers

An array of pointers stores multiple addresses. Useful for:

  • Storing strings of different lengths
  • Creating dynamic tables or lists

Example:

char *names[] = {"Alice", "Bob", "Charlie"};
printf("%s", names[1]);  // Output: Bob

C Pointer to Array

This pointer refers to an entire array, not just the first element.

Syntax:

int (*ptr)[5];  // Pointer to an array of 5 integers

Example:

int arr[5] = {1, 2, 3, 4, 5};
ptr = &arr;
printf("%d", (*ptr)[2]);  // Output: 3

C Pointers to Structures

Pointers can access structure members using the -> operator.

Commonly Used In:

  • Linked lists
  • Binary trees
  • Dynamic records

Example:

struct Person {
    char name[20];
    int age;
};

struct Person p = {"Alice", 25};
struct Person *ptr = &p;

printf("%s", ptr->name);  // Output: Alice

C Pointers with Functions (pass/return)

Pointers allow functions to modify variables outside their scope, simulating pass-by-reference.

Pass Pointer:

void modify(int *n) {
    *n = 100;
}

Return Pointer:

int* getStaticArray() {
    static int arr[3] = {1, 2, 3};
    return arr;
}

C NULL Pointer

A NULL pointer does not point to any valid memory location. It acts as a safe default.

Example:

int *ptr = NULL;
if (ptr == NULL) {
    printf("Pointer is null");
}

๐Ÿ”ณ C void Pointer

A void pointer can point to any data type, making it useful in:

  • Dynamic memory allocation
  • Type-agnostic libraries/APIs

Example:

void *ptr;
int x = 10;
ptr = &x;
printf("%d", *(int *)ptr);

๐Ÿšจ C Dangling Pointer

A dangling pointer points to memory that has been freed or is out of scope.

Avoid This:

int *ptr;
{
    int temp = 5;
    ptr = &temp;
}
// temp is out of scope; ptr is dangling

Fix: Always set pointer to NULL after free() or block exit.


โœด๏ธ C Dereferencing

Dereferencing a pointer accesses the value at its memory address.

Syntax:

int x = 10;
int *p = &x;
printf("%d", *p);  // Output: 10

C Pointer vs Array

FeaturePointersArrays
MemoryCan point anywhereContiguous fixed block
ModifiableYesNo (array name is constant pointer)
sizeofSize of pointerTotal size of array
LifetimeControlledStatic or stack allocated

Conclusion: Arrays and pointers are closely related, but not interchangeable.


Near, Far, and Huge Pointers

Used in 16-bit DOS/embedded systems to manage memory beyond 64KB segments.

Pointer TypeDescription
near16-bit address, within one segment
far32-bit segment:offset address
hugeFar pointer with arithmetic across segments

These are legacy concepts, rarely used in modern C, but relevant in low-level embedded development.


Summary โ€“ Recap & Next Steps

Pointers empower C programmers to write efficient, dynamic, and flexible code by allowing direct memory access and manipulation.

Key Takeaways:

  • Pointers store and manipulate memory addresses
  • Used extensively with arrays, functions, and structures
  • void, NULL, and dangling pointers must be handled safely
  • Pointer arithmetic enables memory navigation
  • Understanding the difference between pointers and arrays is crucial
  • Legacy pointer models (near, far, huge) apply to specific platforms

Real-World Relevance:
Used in embedded systems, kernel-level drivers, dynamic memory managers, and data structures like linked lists and trees.


Frequently Asked Questions (FAQ)


What is a pointer in C?
A pointer is a variable that stores the memory address of another variable.


What is a dangling pointer and how to avoid it?
It points to deallocated memory. Avoid by setting pointer to NULL after free() or block scope exit.


Can a void pointer be dereferenced directly?
No. It must first be cast to a specific type.


How is an array different from a pointer?
Arrays are fixed-size memory blocks; pointers are variables that can point anywhere.


What are near, far, and huge pointers?
Used in 16-bit memory models to address memory beyond segment limits. Rarely used today.


Share Now :
Share

๐Ÿ“ C Pointers

Or Copy Link

CONTENTS
Scroll to Top