char *string
is a pointer to array of char
's. something like this :
.----. .----. .----.
| s | - | a | - | m |
.----. .----. .----.
^
|
char *string --.
it's just like you have an array and an pointer to the array :
char strArr[3];
char *ptrToStr = &strArr;
you can initialize an char *
in various way :
char *string;
scanf("%as", string);
/* if you don't allocate buffer for `*string`, and specify "a" in string format,
then `scanf` will do it for you. this is GNU-only extension. */
char *string2;
string2 = (char *) malloc (BUFF_SIZE);
sprintf(string2, "%s" , "Ghasem Ramezani");
...
But char **string
is an pointer to pointer to char
array. something like this :
.----. .----. .----.
| s | - | a | - | 1 |
.----. .----. .----.
^
|
.-> char *string --.
| .----. .----. .----.
| | s | - | a | - | 2 |
| .----. .----. .----.
| ^
| |
.-> char *string2 --.
|
.-------------.
|
char **strArr ---.
for example of char **
, did you remember argv
argument in main ()
?. that's exactly is type of char **
. let's see an example :
/*1*/ #include <stdio.h>
/*2*/ int main(int argc, char *argv[]) {
/*3*/ for (int i = 0; i < argc; ++i)
/*4*/ puts(argv[i]);
/*5*/ return 0;
/*6*/ }
$> gcc -o output main.c
$> ./output ghasem ramezani
./output
ghasem
ramezani
$>
as you can see in line 2, we can write char *[]
instead of char **
. why ? because as me and @Kevin said :
Read const char** as "a (non-const) pointer to a (non-const) pointer to a constant character".