Reputation: 2758
From my understanding, char* X
is a variable that points to a single character or a character array (string) in C.
char**
is a pointer which points to another pointer which finally points to a single character or a character array.
if int**
is equivalent to creating a multidimensional array, why can't I create an array of strings in C using char**
?
const char** day = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
Here *day
would point to the array itself and **day
would point to the first element of the array "Sunday"?
Upvotes: 0
Views: 8255
Reputation: 213842
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.
Upvotes: 3
Reputation: 11921
why can't I create an array of strings in C using char**? yes you can create. Correct way is to use array of char pointers not the double char**
pointer as you did. For e.g
const char* day[7] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
Above, day
is array of char pointers and its each element is character pointer and each elements needs to point to valid address. Here day[0]
is character pointer & its pointing to string literal sunday
which is valid address.
Its also possible via char**
but not the way as you did. For e.g
char **day = {"sunday", "Monday" };
here problem is you haven't allocated memory for day[0]
, day[1]
to hold some string literal like sunday
& so on.
First allocate memory for day
like below
char **day = malloc(NUM_OF_DAYS * sizeof(*day)); /* define NUM_OF_DAYS as 7 */
And then allocate memory for day[0]
, day[1]
etc.
for(int index =0 ; index < NUM_OF_DAYS; index++) {
day[index] = malloc(MAX_DAY_SIZE * sizeof(**day)); /* define this MACRO */
}
And then you scan the data at run time.
Upvotes: 6