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 2: Pointers and Arrays

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

The Deep Connection

In C, pointers and arrays are nearly the same thing. In fact, the name of an array is actually a constant pointer that points to its very first element.

int arr[5] = {10, 20, 30, 40, 50};
// 'arr' is exactly the same as '&arr[0]'

Pointer Arithmetic

You can perform mathematical operations on pointers! However, they don't work like normal math:

  • ptr + 1 doesn't add 1 byte; it moves the pointer to the next item of that type.
  • If ptr is an int* (4 bytes), ptr + 1 jumps forward 4 bytes.
  • If ptr is a char* (1 byte), ptr + 1 jumps forward 1 byte.

Two Ways to Access Arrays

Because of this connection, these two lines are identical to the C compiler:

  1. arr[i]
  2. *(arr + i)

Array of Pointers

You can have an array where every item is itself a pointer. This is extremely common for handling strings of different lengths.

char *names[] = {"Rahul", "Siksha", "Sarovar"};

Pointer to Pointer (Double Pointer)

A variable that stores the address of another pointer. This is used when you need a function to change where a pointer in another function is pointing.

int x = 5;
int *p = &x;
int **pp = &p; // pp points to p, which points to x

Function Pointers (Advanced)

A pointer can even point to the starting address of a Function! This allows you to pass logic as a parameter to another function (often called "Callbacks").

void (*fptr)(int) = &myFunction;
fptr(10); // Calls the function
Pointer arithmetic is one of the reasons C is so fast. Navigating an array with pointers is often more efficient for the CPU than using indices, although modern compilers are very good at optimizing both.