So the code:
char **three = ( char** )malloc( sizeof( char* ) );
Allocates an array of pointers-to-char, but with only a single element. The variable three
is a pointer-to, a pointer-to a char. Much the same as:
char *three[1];
Normally (as @paxdiablo points out), it would be more usual to allocate a number of pointers:
int line_count = 66;
char **three = ( char** )malloc( line_count * sizeof( char* ) );
Once allocated, this can be used with array notation:
three[0] = "The Owl and the Pussy-cat went to sea";
three[1] = "In a beautiful pea-green boat,";
three[2] = "They took some honey, and plenty of money,";
three[3] = "Wrapped up in a five-pound note."
There's nothing particularly special about a char**
, every C/C++ program gets one as its argv
.
As a programmer, you don't really know that your pointer is 8 bytes, you know the pointer will be some size. This is where the sizeof( char* )
comes in. During compilation the compiler swaps this with the real value. That way when the architecture is 16, 32 or 64 bit (or maybe 128 bit in the future), the code still compiles fine.