Can I create an Array of Char pointers in C? Can I create an Array of Char pointers in C? c c

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.


char *keyword[10];

keyword is an array 10 of char *. In a value context, it converted to a pointer to a char *.

This conversion is a part of what Chris Torek calls "The Rule":

"As noted elsewhere, C has a very important rule about arrays and pointers. This rule -- The Rule -- says that, in a value context, an object of type ‘array of T’ becomes a value of type ‘pointer to T’, pointing to the first element of that array"

See here for more information: http://web.torek.net/torek/c/pa.html

The C-FAQ also has an entry on this array to pointer conversion:

Question 6.3: So what is meant by the "equivalence of pointers and arrays'' in C?

http://c-faq.com/aryptr/aryptrequiv.html


In C, you can't really pass array to a function. Instead, you pass a pointer to the beginning of the array. Since you have array of char*, the function will get a pointer to char*, which is char**.

If you want, you can write (in the prototype) char *keyword[] instead of char **keyword. The compiler will automatically convert it.

Also, in C you can dereference pointers like arrays, so you loose almost nothing with that "converting to pointer".