🟡 Intermediate  ·  Lesson 24

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.

Concept
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

C Language
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
⚠️ Always Initialize Pointers!

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

OperatorNameUsageReturns
&Address-of&variableMemory address of variable
*Dereference (Indirection)*pointerValue at the address stored in pointer
C Language – Complete Pointer Demo
#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;
}
Value of x = 42 Address of x = 0x7ffd5a1234 Value of p = 0x7ffd5a1234 *p (value) = 42 x after *p=100: 100

Pointer Arithmetic

You can add or subtract integers from pointers. The pointer moves by the size of its type.

C Language
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

C Language – Swap using Pointers
#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;
}
Before: x=10, y=20 After: x=20, y=10

Pointers and Arrays

An array name itself is a pointer to its first element!

C Language
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 NULL as 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)
🏋️ Practice

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?
A pointer is a variable that stores the memory address of another variable rather than a value directly. It lets you refer to and change data indirectly, and it is the foundation for arrays, dynamic memory, and passing data efficiently to functions.
How do you declare a pointer in C?
You put a * 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?
The & 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?
A NULL pointer is a pointer that points to nothing, written as 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.
Why are pointers useful in C?
Pointers let functions modify the caller's variables, allow dynamic memory allocation, make it efficient to pass large data without copying, and are the mechanism behind arrays, strings and data structures like linked lists. They are central to how C works.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.