Array of Strings
--
This article is about how to use an array of strings. Check this one if you want to know what an array is. Using string arrays is not a big deal and is the same as using other array data types in new languages. Anyway, it’s different with C, and understanding how a string array is implemented in C helps to understand the behind-the-scenes of the string array implementation in those new languages. To know about strings check this.
As you can see from the image, the array of strings is a 2-dimensional array. The string itself is a 1-dimensional array. So, the array of strings will be a 2-dimensional array. In the image, the names of the cities are stored as a 2-d array. To access a city just use “cities[index]”;
cities[0] // Gives Pune
cities[1] // Gives Mumbai
Declaring a string array
char cities[4][6] = {
"Pune",
"Mumbai",
"Delhi",
"Nagpur"
};
“cities[4][6]” implies that there is a maximum of 4 rows and 6 characters in each row.
Displaying a string from a string array.
printf("%s", cities[0]);
Output
Pune
Quiz program using Array of Strings
We made the quiz program more simple functions here. We can simplify the program, even more, using the string array.
Now, the program is more simplified and has no redundant codes.
It’s the end of this tutorial series. If you are a beginner or someone who would like to understand programming from different perspectives, I hope this series would have benefitted you. These are the core concepts of programming. These concepts are language-independent. If you got these concepts, it would be easy to learn new languages and new concepts like Object-Oriented-Programming.
Feedbacks are always welcome and encouraged. If you got any suggestions or feedbacks use the responses section for that.