Lesson 12·C Programming

Pointers in C

Direct memory addressing, dereferencing, and pointer variables

What is a Pointer?

A Pointer is a special variable that stores the direct memory address of another variable. In C, pointers are designated using the asterisk (*) symbol.

Pointers are derived data types that provide low-level access to memory, allowing efficient array manipulation, dynamic memory allocation, and pass-by-reference semantics.

Memory Address Illustration

Consider the declaration int *ptr; int tot = 95; ptr = &tot;

Memory address allocation diagram for pointers
Pointer variable storing the physical memory address of the integer variable tot.

Features & Capabilities of Pointers

  • A pointer variable must always be declared with the * prefix.
  • Pointers must point to matching data types (mixing types requires explicit casting).
  • Pointers provide the most efficient method for traversing and processing arrays and strings.
  • Enables functions to return multiple values or mutate caller data using addresses.
  • Essential for dynamic memory allocation using malloc(), calloc(), realloc(), and free().
  • Reduces program overhead and improves execution performance significantly.

Practical Pointer Example

Demonstrating the address-of operator (&) and dereference operator (*):

C Example
#include <stdio.h>

void main() {
    int a = 10;
    int *ptr;

    ptr = &a;  // ptr holds the memory address of variable a

    printf("Value of a: %d\n", a);
    printf("Value at *ptr: %d\n", *ptr);
    printf("Address stored in ptr: %p\n", (void*)ptr);
}
Program Output
Value of a: 10
Value at *ptr: 10
Address stored in ptr: 0x7ffd5b92e84c
Note
Using *ptr reads or modifies the value stored at the referenced memory address (dereferencing).