Can I create an Array of Char pointers in C?

It looks like you’re confused by the double stars in

void function(char ** keyword);

The double stars just means that this function expects you to pass a pointer to a pointer to a char. This syntax doesn’t include any information about the fact that you are using an array, or that the char is actually the first char of many in a string. It’s up to you as the programmer to know what kind of data structure this char ** actually points to.

For example, let’s suppose the beginning of your array is stored at address 0x1000. The keyword argument to the function should have a value of 0x1000. If you dereference keyword, you get the first entry in the array, which is a char * that points to the first char in the string “float”. If you dereference the char *, you get the char “f”.

The (contrived) code for that would look like:

void function(char **keyword)
{
    char * first_string = *keyword;   // *keyword is equivalent to keyword[0]
    char first_char = *first_string;  // *first_string is equivalent to first_string[0]
}

There were two pointers in the example above. By adding an offset to the first pointer before dereferencing it, you can access different strings in the array. By adding an offset to the second pointer before dereferencing it, you can access different chars in the string.

Leave a Comment