Difference between char* and char** (in C)

char* is a pointer to char, char ** is a pointer to a pointer to char.

char *ptr; does NOT allocate memory for characters, it allocates memory for a pointer to char.

char arr[10]; allocates 10 characters and arr holds the address of the first character. (though arr is NOT a pointer (not char *) but of type char[10])

For demonstration: char *str = "1234556"; is like:

char *str;         // allocate a space for char pointer on the stack
str = "1234556";   // assign the address of the string literal "1234556" to str

As @Oli Charlesworth commented, if you use a pointer to a constant string, such as in the above example, you should declare the pointer as constconst char *str = "1234556"; so if you try to modify it, which is not allowed, you will get a compile-time error and not a run-time access violation error, such as segmentation fault. If you’re not familiar with that, please look here.

Also see the explanation in the FAQ of newsgroup comp.lang.c.

Leave a Comment