char* X
is a variable that points to a single character or a character array (string) in C.
No, it is a variable that contains the address of a single character. That single character may or may not be the beginning of a null terminated string.
Thus char**
points at a char*
that points at a character.
if int**
is equivalent to creating a multidimensional array
It is not. int**
has nothing to do with multi-dimensional arrays.
why can't I create an array of strings in C using char**
?
Because a char**
is not an array nor can it, strictly speaking, point at one.
A char*
can be used to point at a null terminated string. So if you want an array of strings, you need an array of char*
. Which can for example be written as:
char* day[] = { ... };
Now as it turns out, a char**
can be used to point at the address of the first item of this array, &day[0]
, since that is a char*
. This often make people confuse char**
for multi-dimensional arrays, because it is valid to do this:
char* day[] = { "Sunday", ... };
char** week = &day[0];
printf("%c", week[0][0]); // prints 'S'
But that does not make char**
an array, nor does it make it a 2D array.
You can also use char**
to dynamically allocate a look-up table of strings, where each string has a variable length:
char** week = malloc(7 * sizeof(char*));
for(int i=0; i<7; i++)
{
week[i] = malloc( ... );
}
Here again, the char**
points at the first item of a 1D array of char*
. It is not a 2D array, it does not point at one. That it allows week[i][j]
syntax is irrelevant, it is still not an array (and []
is never actually used on array types, Do pointers support “array style indexing”?).
More info: Correctly allocating multi-dimensional arrays.