Reason for not being able to cast:
void fn1(const char **arrayOfStrings, int arrayLength)
This function takes in an array of pointers.
e.g.
char string1[] = "string1";
const char* arrayOfStrings[] = {string1};
fn1(arrayOfStrings, 1);
You could also write
const char* string1 = "string1";
fn1(&string1, 1);
This is somewhat more cryptic but works the same way. fn1 uses &string1 as an array with one element.
What does not work is writing:
char string1[] = "string1";
const char **temp = &string1;
The reason this does not work is that this tries to set temp, a pointer to a pointer to a pointer.
Is it not possible to cast the character array to a usable (const char **) [of length 1] in a single step?
If you really wanted to write it e.g. for the purpose of understanding c it is possible. Have a look at the following. Just don't use it in a real program.
int main(void)
{
char string1[] = "string1";
char string2[20];
char** string2AsArrayOfPointers = (char**) &string2; // Example for demonstration purposes only. DO NOT USE THIS CODE IN A REAL PROGRAM.
string2AsArrayOfPointers[0] = string1;
printf("result: %s\n", string2AsArrayOfPointers[0]);
return 0;
}
The way this code works is that, string2 which is an array is cast into an array of pointers. The first element is set to point to the location of string1. This enables "string2" to be used as if it was really an array of pointers (note it would be much simpler to simply use use an array of pointers)
"Ok, yeah, I see I think, I always tend to think of C arrays as being equivalent to an unchangable malloc'd pointer of the appropriate size, just arrays are on stack and malloc is on heap, but, yeah, it's not really I suppose."
they are..
Your original thinking is correct. What is going on here relates to how ** works. ** means you are passing a pointer to an array of pointers (or a single pointer). If you try to pass a pointer then the value pointed to by the pointer will be used to lookup the value rather than being used as a value.