Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Pointers Part 1: The Magic of Memory

Lesson 24 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

The Most Powerful (and Feared) Feature

A pointer is simply a special variable that stores the memory address of another variable. While they have a reputation for being difficult, they are the absolute key to high-performance C programming.

Why do we need Pointers?

  1. Dynamic Memory: Allocating memory from the Heap while the program is running.
  2. Efficiency: Passing a huge 1GB structure to a function by sending its 8-byte address instead of copying the whole thing.
  3. Data Structures: Essential for building Linked Lists, Trees, and Graphs.
  4. Hardware Access: Directly reading or writing to hardware registers (common in embedded systems).

The Two Magic Operators

  1. & (Address-of): "Where are you?" It returns the memory address of any variable.
  2. ** (Dereference):* "Follow the arrow." It goes to the address stored in the pointer and retrieves the value at that location.

Declaration and Use

int x = 10;
int *ptr;     // Declaration: ptr is a pointer to an integer
ptr = &x;     // Assign the address of x to ptr

printf("%p", ptr);  // Prints the address (e.g., 0x7ffd...)
printf("%d", *ptr); // Prints the value 10

The NULL Pointer

A pointer that points to nothing (0). Always initialize your pointers to NULL if you don't have a valid address for them yet.

int *p = NULL;
if (p != NULL) {
    // Safe to use
}

Void Pointers (void*)

A "generic" pointer that can point to any type (int, float, char). It's like a universal adapter. However, before you can use it, you must "cast" it back to a specific type.

Segmentation Fault: This is the most common C error. It happens when you try to dereference a NULL or uninitialized "wild" pointer. The computer catches you trying to access memory you don't own and shuts your program down immediately. Always check your pointers!