UMBC CS 201, Fall 05
UMBC CMSC 201 Fall '05
CSEE | 201 | 201 F'05 | lectures | news | help

Arrays of Pointers

Since variables can be pointers, it should come as no surprise that we can create arrays of pointers. We declare variables to be "pointer to int" using int *. We declare arrays using brackets ([ ] ). We can combine them to declare an "array of pointers to int" with int *pInts[5]; This declares pInts to be an array of 5 pointers to int.
that is, pInts[0] can point to some int, pInts[1] can point to another int, and so on. Pictorially, we have

One of the most common uses for arrays of pointers is an array of character pointers. Each character pointer points to a string. We can declare an array of char pointers and initialize them to essentially create an array of strings. For example

char *months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; which we can think of as looking like this:

then we can reference

months[3] to mean "April", An array of "pointers to chars" is used to pass command line arguments to your program.

CSEE | 201 | 201 F'05 | lectures | news | help

Tuesday, 01-Nov-2005 08:17:38 EST