🟡 Intermediate  ·  Lesson 24

Pointers – Introduction

Pointers – परिचय

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.

Pointer क्या है?

Pointer एक variable है जो किसी दूसरे variable का memory address store करता है। Data value (जैसे 42) की बजाय, यह memory में उस location को hold करता है जहाँ value store है।

Concept
int x = 42;    // x value 42 store करता है, address 0x1234 पर
int *p = &x;   // p, x का ADDRESS (0x1234) store करता है
// *  = "pointer to int" declaration में
// &  = "address of x"

Pointers क्यों? ये allow करते हैं:

  • Pass by reference — दूसरे functions में variables modify करना
  • Dynamic memory — runtime पर memory allocate करना
  • Arrays — efficient manipulation
  • Data structures — linked lists, trees

Declare और Initialize

C Language
int   *ip;    // int pointer
float *fp;    // float pointer
int   *np = NULL; // NULL pointer — safe initialization

int age = 25;
int *ptr = &age;   // ptr age को point करता है
⚠️ Pointer हमेशा Initialize करें!

Uninitialized pointer random address contain करता है। Use करने पर crash! हमेशा NULL या valid address से initialize करें।

& और * Operators

C Language – Pointer Demo
int x = 42;
int *p = &x;

printf("x ki value   = %d\\n", x);   // 42
printf("x ka address = %p\\n", &x);  // 0x... (hex)
printf("p ki value   = %p\\n", p);   // Same as &x
printf("*p se value  = %d\\n", *p);  // 42 (dereference)
*p = 100;                           // Pointer se x change karo!
printf("x ab = %d\\n", x);           // x ab 100 hai!
x ki value = 42 x ka address = 0x7ffd5a1234 p ki value = 0x7ffd5a1234 *p se value = 42 x ab = 100

Pointer Arithmetic

Array को Pointer से Traverse करना
int arr[] = {10, 20, 30};
int *p = arr;   // पहले element को point करो
printf("%d\\n", *p);   // 10
p++;            // अगले int पर जाओ (+4 bytes)
printf("%d\\n", *p);   // 20

सारांश

  • Pointer किसी variable का memory address store करता है
  • Declare: int *p;
  • &variable — variable का address मिलता है
  • *pointer — address पर stored value मिलती है (dereference)
  • Use से पहले हमेशा initialize करें (NULL safe है)
  • Array name = first element का pointer
  • Functions में pointer pass करके variables modify करते हैं (pass by reference)
🏋️ Practice

Programs लिखें: (1) 5 variables के address और value print करें (2) Pointers से swap (3) Pointer arithmetic से array traverse (4) Function जो pointer के through min और max return करे।

← Back to C Tutorial