Array of Pointers to Strings in C

In this class, we will try to understand the Array of Pointers to Strings in C.

Array of Pointers to Strings in C

We have already discussed an array of strings in our previous class.

The understanding of the previous class is fundamental because we are considering the same example discussed there.

The image below is the outcome of the previous examples declaration.

Array of Pointers to Strings
Array of Strings

In the above image, the extra null characters are created, which is a waste of space.

To avoid the extra space, we have another way which is an Array of Pointers to Strings.

The Array of Pointers to Strings

The image below is the programming example for the array of pointers to strings.

Array of Pointers to Strings Example
Array of Pointers to Strings Example

The second line of code represents the declaration of The Array of Pointers to Strings.

The variable four_planets is an array of four elements that are pointers to the string constants.

The image below is the visualization for the Array of Pointers to Strings.

Array of Pointers to Strings Visualization
Array of Pointers to Strings Visualization

The only difference between the array of pointers to strings and the array of strings is that the array of pointers to strings is string constants.

We can access the string constants, but we can not modify them.

If we need to modify the characters of the strings, then we have to use the array of strings

As they are string constants, they are to be initialized in the declaration.