An array of strings in C is an array of character arrays, where each character array represents a string. It can be declared and initialized using string literals or character arrays.
Here is an example of declaring and initializing an array of strings using string literals:
#include <stdio.h>
int main() {
// declare and initialize an array of strings with string literals
char *array[] = {"Hello", "World", "!"};
// print the strings in the array
for (int i = 0; i < 3; i++) {
printf("%s\n", array[i]);
}
return 0;
}
In this example, the array of strings is declared and initialized with the strings “Hello”, “World”, and “!”. The size of the array is 3, and the size of each string is determined by the length of the character array that represents it.
The for loop iterates through the elements of the array and prints the strings using the printf function. The output of the program will be:
Hello
World
!
An array of strings can also be declared and initialized using character arrays, like this:
#include <stdio.h>
int main() {
// declare and initialize an array of strings with character arrays
char *array[] = {
{'H', 'e', 'l', 'l', 'o', '\0'},
{'W', 'o', 'r', 'l', 'd', '\0'},
{'!', '\0'}
};
// print the strings in the array
for (int i = 0; i < 3; i++) {
printf("%s\n", array[i]);
}
return 0;
}
In this example, the array of strings is declared and initialized with the same strings as before, but using character arrays instead of string literals. The size of the array is 3, and the size of each string is determined by the length of the character array that represents it.
An array of strings can be used to store and manipulate text data in C language, such as names, addresses, and messages. It is a useful data structure for storing and processing strings in C programs.