Array of Pointers:
Pointer variables may also be declared as an array.
When an array of strings is declared without using a pointer then each element of the array has a fixed length. If the string entered is shorter than the length of the element, then the remaining space of the element cannot be used. Thus memory space is wasted. To solve this problem, the array of strings is declared by using pointer. For example:
char *city[4]={“London” , “New York” , “Hawaii” , “Beijing”};
In the above example, the variable “city” indicates an array of 4 elements. Each element of the variable “city” is of type “pointer to character”. The four strings (names of cities) are initialized into it.
Actually the pointers are stored in the array “city”. Each pointer points to the starting address in memory of each string. The array “city” has a fixed length of four and four strings of any length can be stored in it. The length of the string that can be stored is not fixed.
char city[4] [15];
It will fix 4 elements and each element will have a fixed length of 15 characters, including the null character. Each string will occupy 15 bytes in the computer memory even if the siring is of only one character. In this way a large amount of space is wasted.
comment closed