array of string in c
In C language, an array of strings can be created using a two-dimensional character array. Each element of the array will be a string, represented by a character array.
Here is an example of how to declare and initialize an array of strings in C:
#include <stdio.h>
int main() {
char strings[3][20] = {
"Hello",
"World",
"C Programming"
};
// Accessing and printing the strings
for (int i = 0; i < 3; i++) {
printf("%s\n", strings[i]);
}
return 0;
}
In this example, we have created an array of strings called strings
, which can store a maximum of 3 strings, each with a maximum length of 19 characters (plus the null character \0
at the end).
To access and print the strings, we use a loop and the %s
format specifier in the printf()
function.
Note that in C, strings are represented as character arrays, terminated by a null character \0
. So, when declaring the array, we need to specify the maximum length of each string to ensure enough space is allocated.
I hope this helps! Let me know if you have any further questions.