In C, an array of strings is essentially an array of character pointers (char *
) where each element of the array points to a string (which is an array of characters terminated by a null character '\0'
). Strings in C are handled as arrays of characters, so an array of strings is an array where each element holds a pointer to a character array.
Declaration and Initialization
To declare an array of strings, we can use the following syntax:
This declares an array of pointers to char
, where each pointer in the array points to a string literal.
Here’s a breakdown of the components:
char *arr[]
— an array ofchar *
, meaning an array where each element is a pointer to a character (i.e., a string).{ "Hello", "World", "C", "Programming" }
— the strings that the array of pointers will point to. Each string is a string literal (automatically null-terminated).