Array of Pointers
Array of Pointers : In class you have implemented 2-D arrays as follows : int TwoDArray[3][3]; // a 2 D integer array of order 3 x 3 char CityName[ ][ ] = { “Delhi” , // five cities array
“Kolkata”, // array of strings “Mumbai”, “Chennai”
The above example is implemented in terms of 2D character dimensional array. If you observe the 2-D character Array CityName more closely, then you will find that it is nothing but a 1- D Array of String. Now since we know that a string can be held by a single character pointer, hence to hold five city names we must be supplied with 5 character pointers. More precisely we must be having a 1-D array of pointers to get hold 5 different city names. Following figure will clear the picture :
So the above implementation is based on the concept of Array of Pointers.An Array of string being an array of pointers has all its elements as address of some or the other characters. Now if we want to hold this whole array of string by using only a single pointer can we do it.
Yes of course this can be done, just like you have captured the address of first character of a string into a char pointer, to grab the whole string, you can also use a double pointer to capture the address of first pointer variable of that Array of pointers.
So to hold an Array of strings one can use a double pointer using following code snippet:
char CityName = { “Delhi”, “Kolkata”, “Mumbai”, “Chennai” };
or
char ** CityName = { “Delhi”, “Kolkata”, “Mumbai”, “Chennai” };
Exercise : Try to write a program which will read and write array of strings from user using the above double pointer notation