29.6.25

20250629 16:30 Array of Pointers

                 

← Previous 🏠 Homepage Next Chapter →

                                           


Array of Pointers – A GPS to Data!



What if you had a list that only stored addresses (locations)? 
That’s an array of pointers! Each element is a pointer (basically, a little guy holding a house number).

int *arr[4];
int i = 31, j = 5, k = 19, l = 71;
arr[0] = &i;
arr[1] = &j;
arr[2] = &k;
arr[3] = &l;

Now arr is like a list of spies that tell you where the secret treasure (data) is hidden!
Use *arr[m] to open the treasure chest and see the number inside. 

It’s just like:

“Hey arr[0], where’s i? Oh, 31? Thanks buddy!”

Pointer Array from an Array



You can even create a pointer array that points to different elements of another array:

static int a[] = { 0, 1, 2, 3, 4 };
int *p[] = { a, a + 1, a + 2, a + 3, a + 4 };

Now:

  • p = address list

  • *p = address of a[0]

  • **p = value at that address = 0

Like a pointer-caption! 

Three-Dimensional Array – The Data Cube!





A 3D array is like a box of boxes of boxes. (Matrix inside a matrix inside a matrix!)

int arr[3][4][2] = {
  { {2, 4}, {7, 8}, {3, 4}, {5, 6} },
  { {7, 6}, {3, 4}, {5, 3}, {2, 3} },
  { {8, 9}, {7, 2}, {3, 4}, {5, 1} }
};

This means:

  • 3 layers (think: floors of a building )

  • 4 rows on each floor

  • 2 columns in each row

Want the number 1?

arr[2][3][1]

Break it down:

  • 2 → 3rd floor

  • 3 → 4th row

  • 1 → 2nd column

But wait, pointer magic time!

*( *( *( arr + 2 ) + 3 ) + 1 )

Same result — just sounds like a math wizard’s chant 
Don't worry, it's all memory arithmetic in disguise!

- Summary!



What’s an Array?

An array is a group of similar items stored together.
Think: A row of boxes with numbers on them!

int nums[5] = {10, 20, 30, 40, 50};
  • nums[0] = 10

  • nums[1] = 20

  • *(nums + 1) = 20 (same thing!)

2D Arrays = Tables 

int marks[3][2] = {
  {101, 80},
  {102, 90},
  {103, 85}
};

Looks like a table of students: roll no & marks!

Pointers + Arrays = Magic

  • arr[i] == *(arr + i)

  • arr[i][j] == *(*(arr + i) + j)

Yes, it’s memory wizardry! 

Array of Pointers

You can store addresses in arrays too!

int *ptrs[3];

They point to variables or arrays.

3D Arrays = Data Cubes 

arr[2][3][1] = value

A 3D array is just arrays inside arrays inside arrays!


No comments:

Post a Comment

rating System

Loading...

Understanding Arrays in C

The Bookshelf Analogy & Book-Author Example Arrays are one of the most essential concepts in C programming. They let you store multiple ...