Course Content
Programming in C
About Lesson

At first glance, the difference between an array of pointers and a two-dimensional array may be unclear.

int arr[10][10];
int *ptr[10];

In the above definitions, both arr[1][2] and ptr[1][2] are syntactically legal references to a single int. But, arr is a true two-dimensional array of 100 int-sized locations and 10 x row + col can be used to find the element arr[row][col]. However, for ptr, the definition only allocates 10 pointers and does not initialise them. Assuming that each element of ptr points to a two-element array, then there will be 100 ints set aside, plus 10 cells for pointers. The important advantage of the pointer array is that the rows of the array may have different lengths.

In the previous section, we defined months as an array of char pointers.

static char *months[] = { 
    "Invalid month",
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
  };


We could also have used a two-dimensional array:

static char months_arr[][15] = { 
    "Invalid month",
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
  };

Scroll to Top