I have the following source:
#include <iostream>
using namespace std;
void main(int j)
{
char arr[10][10];
char** ptr;
ptr = arr;
}
when I compile it using VS2010 I get this error:
error : a value of type "char (*)[10]" cannot be assigned to an entity of type "char **"
I thought arrays in c++ were just pointers. So a char[][] could also be char**. What am I doing wrong?
Arrays aren't pointers.
An array decays to a pointer in most circumstances, but this isn't recursive. So a T[] decays to a T *, but a T[][] doesn't decay to a T**.
I suggest reading the whole of the C FAQ chapter on arrays and pointers; in particular, the section on 2D arrays and pointers-to-pointers.
The existing answers, though correct, don't make it very clear that there is a fundamental reason (apart from the language rules) why you cannot cast char [10][10] to char **. Even if you force the cast by saying something like
char arr[2][2];
char ** ptr = (char **)arr;
it won't actually work.
The reason is that in C and C++ a two-dimensional array is laid out in memory as an array of arrays. That is, in C a two-dimensional array is laid out in memory as a single allocation,
arr -> arr[0][0]
arr[0][1]
arr[1][0]
arr[1][1]
You'll notice that arr doesn't point to a char * but to arr[0][0] which is a char; therefore, while arr can be cast to a char *, it cannot be cast to a char **.
The correct forced cast would be
char arr[2][2];
char * ptr = (char *)arr;
If you don't want to force the cast (always a good idea if possible!) you would say
char arr[2][2];
char * ptr = arr[0];
or, to make the outcome clearer,
char arr[2][2];
char * ptr = &arr[0][0];
And you now have (in effect) a pointer to an array of 4 characters. [Proviso: I can't find anything in the C standard that prohibits an implementation from adding padding between two rows of an array, but I don't believe that any real-world implementations do so, and common coding practice depends on the assumption that there will be no such padding.]
If you really needed to convert arr to a char ** you would have to explicitly create an array of pointers:
char arr[2][2]
char * arr_ptrs[2];
char ** ptr;
arr_ptrs[0] = arr[0];
arr_ptrs[1] = arr[1];
ptr = arr_ptrs;
The C language could in principle do this for you automatically if you tried to cast a two-dimensional array to a pointer-to-pointer but that would violate the programmer's expectation that a cast does not have side-effects such as allocating memory.
In Java, by way of comparison, a two-dimensional array is always an array of pointers to arrays, so that the array
char arr[][] = { {'a', 'b'}, {'c', 'd'} };
is laid out in memory as three separate allocations, in arbitrary order and not necessarily adjacent,
arr -> arr[0]
arr[1]
arr[0] -> arr[0][0]
arr[0][1]
arr[1] -> arr[1][0]
arr[1][1]
You will immediately notice that this requires more memory than the equivalent C array, and is slower to evaluate at runtime. On the other hand, it does allow the rows of an array to have different lengths.
The types char[10][10] and char** and char (*)[10] are all different types. However, the first one cannot convert into the second one, it can convert into the third one.
So try this:
char arr[10][10];
char (*ptr)[10];
ptr = arr; //ok
It will work, because as I said object of type char[10][10] can convert into an object of type char (*)[10]. They're compatible types.
I thought arrays in c++ were just pointers.
No, an array is a set of objects laid out contiguously in memory. In some circumstances, they are convertible to a pointer to the first element.
So a char[][] could also be char**
No. It is convertible to a pointer to the first one-dimensional array (which is the type char (*)[10] mentioned in the error message); but that array is not a pointer, so it is not convertible to a pointer-to-pointer.
The error exactly tells you whats wrong a double dimensional array can be assigned to an pointer to array not an double pointer. So what you need is:
char (*ptr)[10] = arr;
What am I doing wrong?
First things first
Arrays are not pointers!! but they act sometimes like pointers.
The rule is:
An expression with array type (which could be an array name) converts to a pointer anytime an array type is not legal, but a pointer type is.
So if you have a single dimensional array:
char arr[10];
Then arr decays to address of the zeroth element it has the type char *.
Hence:
char *ptr = arr;
But if you have an 2 dimensional array which is essentially an array of arrays.
char arr[10][10];
Then arr decays to the pointer to an array of 10 characters.
So, In order to assign arr to something, you will need that something to match the type, which is pointer to an array of characters.
Hence:
char (*ptr)[10] = arr;
Arrays are NOT just pointers -- arrays are arrays. Arrays are not first-class types, however, so you can't use them in many places. The thing that causes your confusion is that array names CAN be implicitly converted into pointers to the array's first element, which means that you can use an array in many places where you need a pointer and it 'just works'. This, however, is not one of those places.
Arrays are not pointers (I notice a lot of books tend to make you think this, though). They are something completely different. A pointer is a memory address while an array is a contiguous set of some data.
In some cases, an array can decay to a pointer to its first element. You can then use pointer arithmetic to iterate through the contiguous memory. An example of this case would be when passing an array to a function as a parameter.
What you probably want to do here is something like:
char arr[10];
char * i = &arr[0];
Obviously you'll need to use 2D arrays and char** in your case. I'll leave that to you to figure out :)
When you cast ar[10][10] to pointer you will get a array of pointer as said above *ar[10] and not **ar.
Related
Could somebody explain the difference between these two function declarations below please? As far I know, aDecay() takes a pointer as an argument and if you have an integer int a[5] you can call aDecay(a) or aDecay(&a[0] because an array decays to a pointer.
Now if I want to call pDecay() I have to use pDecay(&a).
How does pDecay force you to use the &.
void aDecay(int *p)
void pDecay(int (*p)[7])
With plain a (or its equal &a[0]) you have a pointer to a single element in the array. A single element is of type int, so a pointer to it must be int*.
If you have a pointer to the actual array, like you get with &a, you no longer have a pointer to a single int element but to the whole array. The type of the array is int[5], and a pointer to it is int(*)[5].
Also note that in neither case you pass the array by value. You pass a pointer by value.
This is not a pointer to function syntax, it's a pointer to array syntax.
In the first example p is a pointer to an array of integers. p[i] is a single int. It cannot be indexed any further.
In the second example p is a pointer to an array of seven integer arrays. p[i] is an array which can be indexed further, i.e. p[i][0] is valid.
Here is an example of using the second approach:
void fill_data(int (*p)[7], size_t n) {
for (size_t i = 0 ; i != n ; i++) {
for (size_t j = 0 ; j != 7 ; j++) {
p[i][j] = (int)(7*i+j);
}
}
}
int main() {
int data[10][7];
fill_data(data, 10);
}
Demo
aDecay takes a pointer to an int.
An array of int can decay to a pointer to the array's first element.
pDecay takes a pointer to an array of seven ints.
An array does not implicitly convert into a pointer to itself.
Passing p will convert the array p to a pointer instead of keeping it as an array (this is called array decay, because the array decays to a pointer).
Passing &p will not convert p to a pointer, because it will convert the pointer to p to a generic pointer, which is not a big deal because it is actually a pointer.
Why we don't want to convert p to a pointer is because arrays are not just pointers. If you think that arrays are just pointers, try to compare sizeof(myArray) with sizeof(myPointer). An array embeds also its size, not only the pointer to the first element.
First of all, you have a misconception in the question: if a is an array, a is always a pointer to that array, i.e. you do pDecay(a) - NOT pDecay(&a). Now, [ ] is a dereferencing operation, so if you say a[5] - you dereference a pointer to a + 5 * bytes in memory occupied by the array's unit. Accordingly, a and &a[0] is exactly the same. a[5] and &(a + 5) is the same.
Answering your question, aDecay(int *p) takes a pointer to integer - nothing asks for an array here. int (*p)[7] is an array of 7 integers and compiler will check that's the case.
How does pDecay force you to use the &.
Because an array doesn't decay to just any pointer. It specifically decays to pointer to first element. The type of pointer to first element of an array of int (i.e. int[n]) is: pointer to int (i.e. int*).
The argument of pDecay is int (*p)[7] which is a pointer to an array of 7 int. An int* is not implicitly convertible to (int (*p)[7]; they are separate, incompatible types. Note that pDecay(&a) won't work either because the type of &a is (int (*p)[5] which is also a different type than (int (*p)[7].
pDecay doesn't force you to use & in general. You could pass an array of arrays, which would then decay to a pointer to first array, which is what pDecay accepts:
int arr[n][7];
pDecay(arr);
Could somebody explain the difference between these two function declarations below please?
int* is a pointer to an int object. int (*p)[7] is a pointer to an int[7] object i.e. an array of 7 int.
I am learning c pointer and I follow c4learn as tutorial. In pointer to array of string section, which has following code:
char *arr[4] = {"C","C++","Java","VBA"};
char *(*ptr)[4] = &arr;
I didn't get what is
*(*ptr)[4]
? Wasn't it possible to use it like
**ptr
instead?
Update1:
Currently I am in the next section, function pointer and I saw again similar code:
void *(*ptr)();
char *(*ptr)[4]
is a pointer to a length 4 array of pointers to char (char*). Since arr is a length 4 array of char*, ptr can be made to point to arr by assigning it the address of arr, &arr.
void *(*ptr)();
Is a pointer to a parameterless function returning void*. For example
void* fun(); // function
void *(*ptr)(); // function pointer
p = fun; // function pointer points to function
C syntax can be quite confusing, so it may be easier to illustrate this with some examples. Note that whitespaces between T and ; make no difference.
T name[N]; // size N array of T
T * name[N]; // size N array of pointer to T
T (*name)[N]; // pointer to size N array of T
T ** name[N]; // size N array of pointer to pointer to T
T *(*name)[N]; // pointer to size N array of pointer to T
char *ar[4];
Declares ar as an array of four pointers to chars.
To declare a pointer, you take the declaration of something it can point to, and replace the variable name with (*some_other_name).
So char *(*ptr)[4]; declares ptr as a pointer to an array of four pointers to chars. Normally you can drop the brackets, but in this case, char **ptr[4]; would declare ptr as an array of four pointers to pointers to chars which is not what we want.
Similarly for a function pointer. void *fn() declares a function. void *(*ptr)() declares a pointer that could point to fn. void **ptr() would declare a function with a different return type.
Wasn't it possible to use it like **ptr instead?
Yes, assuming you mean like ptr2 below:
const char* arr[4] = {"C","C++","Java","VBA"};
const char* (*ptr)[4] = &arr;
const char** ptr2 = arr;
There is a difference though... with ptr the type still encodes the array length, so you can pass ptr but not ptr2 to functions like the one below:
template <size_t N>
void f(const char* (&arr)[N]) { ...can use N in implementation... }
Currently I am in the next section, function pointer and I saw again similar code: void *(*ptr)();
That creates a pointer to a function - taking no arguments - returning a void* (i.e. the address of an unspecified type of data, or nullptr).
char *(*ptr)[4] is an array pointer to an array of pointers.
With less obfuscated syntax: if you have a plain array int arr[4]; then you can have an array pointer to such an array by declaring int (*arr_ptr)[4].
So there are arrays, regular pointers and array pointers. Things get confusing because when you use the array name by itself, arr, it decays into a regular pointer to the first element. Similarly, if you have a regular pointer and let it point at the array, ptr = arr; it actually just points at the first element of the array.
Array pointers on the other hand, points at the "whole array". If you take sizeof(*arr_ptr) from the example above, you would get 4*sizeof(int), 4*4=16 bytes on a 32-bit machine.
It should be noted that an array pointer a mildly useful thing to have. If you are a beginner, you don't really need to waste your time trying to understand what this is. Array pointers are mainly there for language consistency reasons. The only real practical use for array pointers is pointer arithmetic on arrays-of-arrays, and dynamic allocation of multi-dimensional arrays.
int (*arr)[5] means arr is a pointer-to-an-array of 5 integers. Now what exactly is this pointer?
Is it the same if I declare int arr[5] where arr is the pointer to the first element?
Is arr from both the examples are the same? If not, then what exactly is a pointer-to-an-array?
Theory
First off some theory (you can skip to the "Answers" section but I suggest you to read this as well):
int arr[5]
this is an array and "arr" is not the pointer to the first element of the array. Under specific circumstances (i.e. passing them as lvalues to a function) they decay into pointers: you lose the ability of calling sizeof on them.
Under normal circumstances an array is an array and a pointer is a pointer and they're two totally different things.
When dealing with a decayed pointer and the pointer to the array you wrote, they behave exactly the same but there's a caveat: an array of type T can decay into a pointer of type T, but only once (or one level-deep). The newly created decayed type cannot further decay into anything else.
This means that a bidimensional array like
int array1[2][2] = {{0, 1}, {2, 3}};
can't be passed to
void function1(int **a);
because it would imply a two-levels decaying and that's not allowed (you lose how elements of the array are laid out). The followings would instead work:
void function1(int a[][2]);
void function1(int a[2][2]);
In the case of a 1-dimensional array passed as lvalue to a function you can have it decayed into a simple pointer and in that case you can use it as you would with any other pointer.
Answers
Answering your questions:
int (*arr)[5]
this is a pointer to an array and you can think of the "being an array of 5 integers" as being its type, i.e. you can't use it to point to an array of 3 integers.
int arr[5]
this is an array and will always behave as an array except when you pass it as an lvalue
int* ptrToArr = arr;
in that case the array decays (with all the exceptions above I cited) and you get a pointer and you can use it as you want.
And: no, they're not equal otherwise something like this would be allowed
int (*arr)[5]
int* ptrToArr = arr; // NOT ALLOWED
Error cannot convert ‘int (*)[5]’ to ‘int*’ in initialization
they're both pointers but the difference is in their type.
At runtime, a pointer is a "just a pointer" regardless of what it points to, the difference is a semantic one; pointer-to-array conveys a different meaning (to the compiler) compared with pointer-to-element
When dealing with a pointer-to-array, you are pointing to an array of a specified size - and the compiler will ensure that you can only point-to an array of that size.
i.e. this code will compile
int theArray[5];
int (*ptrToArray)[5];
ptrToArray = &theArray; // OK
but this will break:
int anotherArray[10];
int (*ptrToArray)[5];
ptrToArray = &anotherArray; // ERROR!
When dealing with a pointer-to-element, you may point to any object in memory with a matching type. (It doesn't necessarily even need to be in an array; the compiler will not make any assumptions or restrict you in any way)
i.e.
int theArray[5];
int* ptrToElement = &theArray[0]; // OK - Pointer-to element 0
and..
int anotherArray[10];
int* ptrToElement = &anotherArray[0]; // Also OK!
In summary, the data type int* does not imply any knowledge of an array, however the data type int (*)[5] implies an array, which must contain exactly 5 elements.
A pointer to an array is a pointer to an array of a certain type. The type includes the type of the elements, as well as the size. You cannot assign an array of a different type to it:
int (*arr)[5];
int a[5];
arr = &a; // OK
int b[42];
arr = &b; // ERROR: b is not of type int[5].
A pointer to the first element of an array can point to the beginning of any array with the right type of element (in fact, it can point to any element in the array):
int* arr;
int a[5];
arr = &a[0]; // OK
int b[42];
arr = &b[0]; // OK
arr = &b[9]; // OK
Note that in C and C++, arrays decay to pointers to the type of their elements in certain contexts. This is why it is possible to do this:
int* arr;
int a[5];
arr = a; // OK, a decays to int*, points to &a[0]
Here, the type of arr (int*) is not the same as that of a (int[5]), but a decays to an int* pointing to its first element, making the assignment legal.
Pointer to array and pointer to first element of array both are different. In case of int (*arr)[5], arr is pointer to chunk of memory of 5 int. Dereferencing arr will give the entire row. In case of int arr[5], arr decays to pointer to first element. Dereferencing arr will give the first element.
In both cases starting address is same but both the pointers are of different type.
Is it the same if i declare int arr[5] where arr is the pointer to the first element? is arr from both example are same? if not, then what exactly is a pointer to an array?
No. To understand this see the diagram for the function1:
void f(void) {
int matrix[4][2] = { {0,1}, {2,3}, {4,5}, {6,7} };
char s[] = "abc";
int i = 123;
int *p1 = &matrix[0][0];
int (*p2)[2] = &matrix[0];
int (*p3)[4][2] = &matrix;
/* code goes here */
}
All three pointers certainly allow you to locate the 0 in matrix[0][0], and if you convert these pointers to ‘byte addresses’ and print them out with a %p directive in printf(), all three are quite likely to produce the same output (on a typical modern computer). But the int * pointer, p1, points only to a single int, as circled in black. The red pointer, p2, whose type is int (*)[2], points to two ints, and the blue pointer -- the one that points to the entire matrix -- really does point to the entire matrix.
These differences affect the results of both pointer arithmetic and the unary * (indirection) operator. Since p1 points to a single int, p1 + 1 moves forward by a single int. The black circle1 is only as big as one int, and *(p1 + 1) is just the next int, whose value is 1. Likewise, sizeof *p1 is just sizeof(int) (probably 4).
Since p2 points to an entire ‘array 2 of int’, however, p2 + 1 will move forward by one such array. The result would be a pointer pointing to a red circle going around the {2,3} pair. Since the result of an indirection operator is an object, *(p2 + 1) is that entire array object, which may fall under The Rule. If it does fall under The Rule, the object will become instead a pointer to its first element, i.e., the int currently holding 2. If it does not fall under The Rule -- for instance, in sizeof *(p2 + 1), which puts the object in object context -- it will remain the entire array object. This means that sizeof *(p2 + 1) (and sizeof *p2 as well, of course) is sizeof(int[2]) (probably 8).
1 Above content has been taken from More Words about Arrays and Pointers.
The address of the whole array, and the address of the first element, are defined to be the same, since arrays in C++ (and C) have no intrinsic padding besides that of the constituent objects.
However, the types of these pointers are different. Until you perform some kind of typecast, comparing an int * to an int (*)[5] is apples to oranges.
If you declare arr[5], then arr is not a pointer to the first element. It is the array object. You can observe this as sizeof( arr ) will be equal to 5 * sizeof (int). An array object implicitly converts to a pointer to its first element.
A pointer to an array does not implicitly convert to anything, which may be the other cause of your confusion.
If you write int arr[5], you are creating an array of five int on the stack. This takes up size equal to the size of five ints.
If you write int (*arr)[5], you are creating a pointer to an array of five int on the stack. This takes up size equal to the size of a pointer.
If it is not clear from the above, the pointer has separate storage from the array, and can point at anything, but the array name cannot be assigned to point at something else.
See my answer here for more details.
I was fidgeting with the pointers thingy.graduated from pointers -> array of pointers -> function pointers -> pointer to pointers..
Here's what i am stuck at...mostly the convoluted syntax.
Lets say i have an array of integers.
int arr[4] = {1,2,3..};
also i have array of pointers
int* ptr[4];
ptr[0] = arr;
here ptr[0] will point to 1
and ptr[1] will point to some other location
This works perfectly !!
Now considering above background i tried this.
char* crr[4] ={"C","C++","C#","F#"};
char** btr[4];
btr[0] = crr;
which works as pointer in oth element of btr is pointing to another pointer element in crr.
Then i tried this.
char* crr[4] ={"C","C++","Java","VBA"};
char** btr[4]= &crr; // Exception: cannot convert char* [4] * to char**[4]
but when i do this it works :O
char* crr[4] ={"C","C++","Java","VBA"};
char* (*btr)[4]= &crr;
i have not understood the last two scenarios. The use of brackets on RHS Pls explain.
char** btr[4]= &crr; // Exception: cannot convert char* [4] * to char**[4]
That's trying to initialise an array (of pointers to pointers) from a pointer, which you can't do. If you wanted to initialise the first element to point to crr (as in your first example), then you'd do
char** btr[4]= {crr};
The last example is declaring a pointer to an array (of pointers), and initialising it from a compatible pointer.
Note that your original array should be of const char *, not char *, since it contains pointers to (constant) string literals.
Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.
When you write
btr[0] = crr;
the expression crr has type "4-element array of char *"; since it is not the operand of a sizeof or & operator, it is converted to type "pointer to char *", or char **. This matches the type of btr[0].
However, when you write
char** btr[4]= &crr;
the expression crr is the operand of the & operator; thus, the type of the expression &crr is "pointer to 4-element array of char *", or char *(*)[4].
The parentheses are necessary because the postfix [] operator has higher precedence than the unary * operator; the expression *a[i] will always be parsed as *(a[i]). Thus,
T *a[N]; // a is an N-element array of pointer to T
T (*a)[N]; // a is a pointer to an N-element array of T
T *(*a)[N]; // a is a pointer to an N-element array of pointer to T
The same is true for pointers and functions:
T *f(); // f is a function returning a pointer to T
T (*f)(); // f is a pointer to a function returning T
T *(*f)(); // f is a pointer to a function returning pointer to T
Let's look at your definitions:
Array of size 4 of char*
char* crr[4] ={"C","C++","Java","VBA"};
Array of size 4 of char**
char** btr[4]
Pointer to an array of size 4 of char*
char* (*btr)[4]
Now you are trying to assign Array of size 4 of char* to Array of size 4 of char** and they are of different type, so you, obviously, have a error. Still there is another rule(which is dominated): you can't initialize array without curly braces, so array should be initialized with new data not to be a pointer to other array.
And you have absoulutely legal pointer type Pointer to an array of size 4 of char* to which your array address could be assigned
I have a hard time understanding or finding a simple explanation of what const char** x is and how to use it?
I kind of understand that char* x[] is an array of char pointers.
One explanation for the char** x I found says that "they are variable pointers to an array of variable pointers to const chars".
I am confused. Is x an array of pointers or just one pointer that points to an array of pointers?
If I wrote T *x, you'd understand that, I assume? x is simply a pointer to an object of type T. And that object may be the first element in an array of T. Whereas T x[10] really is an array of 10 T.
In your case, T is const char *. So x is a pointer to a const char *.
[] it mostly a shortcut for declaring arrays of thing, and not really a type. It is used when declaring the elements of the array, or specifying how many "slots" that array have.
But C don't handle entire arrays as if it was one single thing, so the resultant variable you get from that operation is a char * pointer pointing to the first element of that array, so that char x[] and char *x is somewhat implied of being the same type.
When declaring char x[] = "string";, for instance, x is a variable of type char * that points to the first element of the array, x[0] which value is 's'.
You can reapply the concept to a matrix or an array of arrays, which is the same as a pointer to a collecion of pointers.
So while char *x may be a string, char **x may be a list of strings.
for every type you use * it is a pointer to that type. it might point to one element or to the first element (at the first time you assign it) of a sequence of elements on the memory. with a pointer you can use pointer arithmetik for example x++ to navigate to the next element, you can also use the index operator x[1] on it. pointer arithmetik cannot be applied to a const pointer, witch is obvious since you declared it to be constant.
for every type you use [] it is an array. with an array you cannot use pointer arithmetik, only the the [] operator lets you access the elements of an array.
you can say that a const pointer is similar the an array when it comes to accessing element.
Use array when you know at compile time what size it should have. these will reside on the stack.
Use pointers when you have to allocate memory at runtime or you wich to use pointer arithmetik. memory allocated at runtime resides on the heap.
here a lecture that might help you understand better.
I hope i didn't tell any mistakes, it's been a while since i used C the last time :/
Here's one example:
char ch;
char *pch = &ch;
char **ppch = &pch;
In this case, no array is involved.
There is one variable that holds a character (ch).
Then there is one variable that holds a pointer (pch). This is simply an address of a location in memory that holds data. But the compiler and the programmer are also interested in the type of the value that is stored in that location (for example, to know how many bytes it takes, or which operations are allowed on that value). So the pointer has a type that is related to the type of the value it's pointing to. In this case, pch is pointing to a character value, so the type is char *.
And then there's another variable that holds a pointer (ppch). Again it's just an address of a location in memory, but this time the value that is stored in that location is a pointer. So there are two pointers involved, ppch and the pointer it's pointing to. Both pointers have a type. ppch is a char ** - pointer to a pointer to char. The value it's pointing to is a char * - pointer to a character.
A pointer can point to a single value, but in some cases it's also useful if there is a sequence of values with the same type (an array). In that case, the pointer can point to any of the values in the sequence, and using pointer arithmetic it can be changed to point to other values in the same sequence.
So just like char * is a pointer that can point to a character, it can also point to any value in a sequence of a characters, and in many cases it's useful if it points to the first value in the sequence.
char ** points to a char * value, which again can be a single value or one of many in a sequence of values (and more specifically, the first in the sequence).
This is NOT the same as char *array[] which is the actual sequence of pointers. The difference is important:
char *pointers[5];
char **pp;
Here, char *pointers[5] is an array of 5 pointers. It's the actual sequence. sizeof(pointers) is 5 times the size of a single pointer.
But pp is just a pointer - it's a variable that holds a single pointer, and sizeof(pp) is the size of a single pointer. As it's a pointer to pointer, it can also point to the first element of pointers (or to any other char * value, including other elements of pointers).
Now there's only the question of const. This only affects the type that it appears with (this is a bit confusing, because C allows different orders, and const char is the same as char const). So const char **pp is a non-const pointer (meaning the pointer can change) to a non-const pointer (so that pointer can also change) to a const char (meaning that the value of the character cannot be modified by pp).
So pp itself can be changed by the program - it can point to different pointers. The pointer that it's pointing to can also change - it can point to different characters. But the characters themselves cannot be changed using pp.
For a reasonable example, we need a few const char pointers, and a few characters to point to. The easiest way to get those is to use arrays:
char seq1[5] = { 'a', 'b', 'c', 'd', 'e' };
char seq2[3] = { 'x', 'y', 'z' };
const char *p1;
const char *p2;
const char **pp;
p1 = &seq1[0]; /* valid, same as p1 = seq1 */
p1 = &seq1[3]; /* valid */
*p1 = 's'; /* invalid - the character that p1 points to cannot be changed by p1 */
p2 = seq2; /* valid */
pp = &p1; /* valid */
*pp = &seq1[2]; /* valid - pp is a non-const pointer, so the value it points to can change */
pp = &p2; /* valid - pp itself is non-const */
**pp = 't'; /* invalid - same as *(*pp) which is *p2 which is const when accessed by pp */
Note that the meaning of const is not that the value cannot ever change, but that it cannot be changed by the const variable (or the const pointer). The following example should clarify:
char ch;
char *pch = &ch;
const char *cpch = &ch;
ch = 'a'; /* fine - ch is not const */
*pch = 'b'; /* fine - pch does not point to a const value */
*cpch = 'c'; /* invalid - cpch points to a const value */