Skip to content
StudyFame

Array of Pointers in C

An array stores many values of one type. An array of pointers stores many addresses. Each element is a pointer that can point to a different variable or a different string.

In this lesson you will declare arrays of pointers, use them to keep a list of strings without wasting memory, and sort names by swapping only the pointers, a neat trick that's much faster than copying strings.

What You Will Learn

  • What an array of pointers is.
  • How to declare and initialize it.
  • How to reach values through *arr[i].
  • How to store a list of strings with char *names[].
  • How to sort strings by swapping pointers.
  • Array of pointers vs pointer to an array.

What is an Array of Pointers in C?

Exam definition: An array of pointers is an array whose every element is a pointer. In int *arr[5];, arr is an array of 5 elements, and each element is an int * that can hold the address of an int.

Memory trick: An array of pointers is like a phone's contact list: the list itself doesn't hold the people, only their addresses. Re-ordering the contact list doesn't move anyone's house.

int a = 10, b = 20, c = 30;
int *arr[3] = {&a, &b, &c};

 arr[0]   arr[1]   arr[2]
+-------+-------+-------+
| &a    | &b    | &c    |
+-------+-------+-------+
    |       |       |
    v       v       v
   a=10    b=20    c=30       *arr[1] = 20

Syntax of an Array of Pointers

int  *arr[5];                         /* 5 pointers to int  */
char *names[3] = {"Asha", "Ravi", "Neha"};   /* 3 pointers to strings */

arr[0] = &x;      /* store an address    */
*arr[0] = 7;      /* change x through it */

Read int *arr[5] from the name outward: arr is an array of 5 (brackets bind first) … of pointers to int.

Example 1: Pointers to Separate Variables

#include <stdio.h>

int main(void)
{
    int maths = 78, physics = 64, chemistry = 85;
    int *marks[3] = {&maths, &physics, &chemistry};
    const char *subject[3] = {"Maths", "Physics", "Chemistry"};
    int i, total = 0;

    for (i = 0; i < 3; i++)
    {
        *marks[i] += 5;                /* grace marks to each subject */
        total += *marks[i];
        printf("%-10s %d\n", subject[i], *marks[i]);
    }
    printf("Total      %d\n", total);
    printf("physics variable is now %d\n", physics);

    return 0;
}

Output

Maths      83
Physics    69
Chemistry  90
Total      242
physics variable is now 69

Code Explanation

  1. int *marks[3] holds the addresses of three separate variables, not stored side by side.
  2. *marks[i] means *(marks[i]): take pointer number i, then go to the value. += 5 changes the real variable.
  3. subject is an array of string pointers, a very common use. Each element points to a string literal.
  4. The last line proves the original variable physics really changed.

How an Array of String Pointers Saves Memory

char a[3][10] = {"Jan", "February", "May"};     char *p[3] = {"Jan", "February", "May"};

|J|a|n|\0| | | | | | |   10 bytes               p[0] -> "Jan"       4 bytes
|F|e|b|r|u|a|r|y|\0| |   10 bytes               p[1] -> "February"  9 bytes
|M|a|y|\0| | | | | | |   10 bytes               p[2] -> "May"       4 bytes
total: 30 bytes (wasted gaps)                   total: 17 bytes + 3 pointers
  • A 2D char array gives every row the same width, wasting space on short words.
  • An array of pointers lets each string use exactly the bytes it needs. This is called a jagged (ragged) array.
  • The catch: the strings are literals, so they are read-only.

Example 2: Sort Names by Swapping Pointers

With a 2D char array, sorting needs three strcpy calls per swap. With an array of pointers, you swap only two addresses. The strings never move.

#include <stdio.h>
#include <string.h>

int main(void)
{
    const char *names[] = {"Rohan", "Anjali", "Vikram", "Meena", "Deepak"};
    int n = sizeof(names) / sizeof(names[0]);
    const char *temp;
    int i, j;

    for (i = 0; i < n - 1; i++)
    {
        for (j = 0; j < n - 1 - i; j++)
        {
            if (strcmp(names[j], names[j + 1]) > 0)
            {
                temp = names[j];          /* swap the pointers only */
                names[j] = names[j + 1];
                names[j + 1] = temp;
            }
        }
    }

    for (i = 0; i < n; i++)
        printf("%d. %s (%zu letters)\n", i + 1, names[i], strlen(names[i]));

    return 0;
}

Output

1. Anjali (6 letters)
2. Deepak (6 letters)
3. Meena (5 letters)
4. Rohan (5 letters)
5. Vikram (6 letters)
  • sizeof(names) / sizeof(names[0]) works here because names is a real array (of pointers).
  • The swap uses = because we're copying addresses, which is allowed. Compare this with the strcpy version in Array of Strings.
  • Swapping 8-byte pointers is faster than copying whole strings, especially for long text.

Array of Pointers vs Pointer to an Array

DeclarationMeaningsizeof (64-bit)
int *p[3];Array of 3 pointers to int24 (3 × 8)
int (*p)[3];One pointer to an array of 3 ints8

Rule: [] binds tighter than *. Without brackets, the name is an array first. With (*p), the name is a pointer first.

Important Points to Remember

  • int *arr[n] = an array of n pointers.
  • *arr[i] = the value that pointer i points to.
  • Pointers in the array can point to scattered variables.
  • char *names[] stores strings without wasted space (jagged array).
  • Sorting strings? Swap the pointers, not the strings.
  • int *p[3] (array of pointers) ≠ int (*p)[3] (pointer to array).

Common Mistakes Beginners Make

MistakeWhat happensFix
Using *arr[i] before setting arr[i]Wild pointer: crashAssign an address first
scanf("%s", names[i]) with char *names[5]Pointers have no memory: crashUse char names[5][20] or malloc
Changing letters: names[0][0] = 'X' on literalsCrashUse a 2D char array if editing is needed
Confusing int *p[3] with int (*p)[3]Type errorsRemember: [] binds first
Using strcpy to swap pointer-array stringsWrites into read-only literalsSwap the pointers with =

Array of Pointers Practice Questions

  1. Declare an array of 4 pointers to float.
  2. What is the size of char *list[6] on a 64-bit system?
  3. Why does char *p[] use less memory than char a[][20] for a list of words?
  4. Why is sorting with an array of pointers faster?
  5. What is the difference between int *p[3] and int (*p)[3]?
Show answers
  1. float *fp[4];
  2. 6 × 8 = 48 bytes.
  3. Each string takes only its own length + 1 bytes instead of a fixed row width.
  4. Only two addresses are swapped; the characters are never copied.
  5. The first is an array of 3 int pointers; the second is one pointer to an array of 3 ints.

Coding Exercise

Create const char *days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};. Print the days in reverse. Then re-point days[2] and days[5] to "Wednesday" and "Saturday", and print the longest day name in the list.

Show solution
#include <stdio.h>
#include <string.h>

int main(void)
{
    const char *days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
    int i, longest = 0;

    for (i = 6; i >= 0; i--)
        printf("%s ", days[i]);
    printf("\n");

    days[2] = "Wednesday";          /* re-point, not copy */
    days[5] = "Saturday";

    for (i = 1; i < 7; i++)
        if (strlen(days[i]) > strlen(days[longest]))
            longest = i;
    printf("Longest now: %s\n", days[longest]);
    return 0;
}
Sun Sat Fri Thu Wed Tue Mon
Longest now: Wednesday

Quick Quiz: Array of Pointers (MCQ)

  1. int *a[4]; declares:

    A) A pointer to 4 ints   B) An array of 4 int pointers   C) 4 ints   D) A function

  2. int x = 5; int *a[1] = {&x}; *a[0] = 9; Now x is:

    A) 5   B) 9   C) Address   D) Error

  3. Main memory advantage of char *s[] over char s[][20]:

    A) Editable strings   B) No wasted space per string   C) Faster input   D) None

  4. To sort strings in an array of pointers, we swap:

    A) Characters   B) Pointers   C) Lengths   D) Nothing

  5. sizeof(int *[5]) on a 64-bit system:

    A) 5   B) 20   C) 40   D) 8

Show answers
  1. B: an array of 4 int pointers.
  2. B: 9.
  3. B: no wasted space.
  4. B: pointers.
  5. C: 40.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

int *arr[n]Array of n pointers to int
*arr[i]Value at pointer i
char *names[]List of strings, no wasted space, literals read-only
Sorting stringsSwap pointers (fast)
int (*p)[3]Pointer to array (different!)
Binding rule[] before *

Frequently asked questions

What is an array of pointers?
An array in which every element is a pointer, e.g. int *arr[5].
What is the difference between an array of pointers and a pointer to an array?
int *p[3] is three pointers; int (*p)[3] is one pointer to a block of three ints.
Why use an array of pointers for strings?
It saves memory (jagged rows) and makes sorting fast by swapping addresses.

Remember

  • Array of pointers = contact list of addresses.
  • Re-order the list, not the houses.

Next Topic

Next lesson: Function Pointers in C. You will store the address of a function and call it through a pointer.